Delete directory regardless of 260 char limit

2019-01-16 23:10发布

I'm writing a simple script to delete USMT migration folders after a certain amount of days:

## Server List ##
$servers = "Delorean","Adelaide","Brisbane","Melbourne","Newcastle","Perth"

## Number of days (-3 is over three days ago) ##
$days = -3

$timelimit = (Get-Date).AddDays($days)

foreach ($server in $servers)
{
    $deletedusers = @()
    $folders = Get-ChildItem \\$server\USMT$ | where {$_.psiscontainer}
    write-host "Checking server : " $server
    foreach ($folder in $folders) 
    {
        If ($folder.LastWriteTime -lt $timelimit -And $folder -ne $null)
        {
            $deletedusers += $folder
            Remove-Item -recurse -force $folder.fullname
        }
    }
        write-host "Users deleted : " $deletedusers
        write-host
}

However I keep hitting the dreaded Remove-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters.

I've been looking at workarounds and alternatives but they all revolve around me caring what is in the folder.

I was hoping for a more simple solution as I don't really care about the folder contents if it is marked for deletion.

Is there any native Powershell cmdlet other than Remove-Item -recurse that can accomplish what I'm after?

17条回答
小情绪 Triste *
2楼-- · 2019-01-16 23:54

I often have this issue with node projects. They nest their dependencies and once git cloned, it's difficult to delete them. A nice node utility I came across is rimraf.

npm install rimraf -g
rimraf <dir>
查看更多
家丑人穷心不美
3楼-- · 2019-01-16 23:55

I've created a PowerShell function that is able to delete a long path (>260) using the mentioned robocopy technique:

function Remove-PathToLongDirectory 
{
    Param(
        [string]$directory
    )

    # create a temporary (empty) directory
    $parent = [System.IO.Path]::GetTempPath()
    [string] $name = [System.Guid]::NewGuid()
    $tempDirectory = New-Item -ItemType Directory -Path (Join-Path $parent $name)

    robocopy /MIR $tempDirectory.FullName $directory | out-null
    Remove-Item $directory -Force | out-null
    Remove-Item $tempDirectory -Force | out-null
}

Usage example:

Remove-PathToLongDirectory c:\yourlongPath
查看更多
Fickle 薄情
4楼-- · 2019-01-16 23:55

If all you're doing is deleting the files, I use a function to shorten the names, then I delete.

    function ConvertTo-ShortNames{
    param ([string]$folder)
    $name = 1
    $items = Get-ChildItem -path $folder
    foreach ($item in $items){
        Rename-Item -Path $item.FullName -NewName "$name"
        if ($item.PSIsContainer){
            $parts = $item.FullName.Split("\")
            $folderPath = $parts[0]
            for ($i = 1; $i -lt $parts.Count - 1; $i++){
                $folderPath = $folderPath + "\" + $parts[$i]
            }
            $folderPath = $folderPath + "\$name"
            ConvertTo-ShortNames $folderPath
        }
        $name++
    }
}

I know this is an old question, but I thought I would put this here in case somebody needed it.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-16 23:55

I had the same issue while trying to delete folders on a remote machine.

Nothing helped but... I found one trick :

# 1:let's create an empty folder
md ".\Empty" -erroraction silentlycontinue

# 2: let's MIR to the folder to delete : this will empty the folder completely.
robocopy ".\Empty" $foldertodelete /MIR /LOG+:$logname 

# 3: let's delete the empty folder now:
remove-item $foldertodelete -force

# 4: we can delete now the empty folder
remove-item ".\Empty" -force

Works like a charm on local or remote folders (using UNC path)

查看更多
干净又极端
6楼-- · 2019-01-17 00:00

If you have ruby installed, you can use Fileman:

gem install fileman

Once installed, you can simply run the following in your command prompt:

fm rm your_folder_path

This problem is a real pain in the neck when you're developing in node.js on Windows, so fileman becomes really handy to delete all the garbage once in a while

查看更多
唯我独甜
7楼-- · 2019-01-17 00:00

Adding to Daniel Lee's solution, When the $myDir has spaces in the middle it gives FILE NOT FOUND errors considering set of files splitted from space. To overcome this use quotations around the variable and put powershell escape character to skip the quatations.

PS>cmd.exe /C "rmdir /s /q <grave-accent>"$myDir<grave-accent>""

Please substitute the proper grave-accent character instead of <grave-accent>
SO plays with me and I can't add it :). Hope some one will update it for others to understand easily

查看更多
登录 后发表回答