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条回答
姐就是有狂的资本
2楼-- · 2019-01-16 23:45

Any thing developed using .NET out of the box will fail with paths too long. You will have to move them to 8.3 names, PInVoke (Win32) calls, or use robocopy

查看更多
来,给爷笑一个
3楼-- · 2019-01-16 23:48

This is a known limitation of PowerShell. The work around is to use dir cmd (sorry, but this is true).

http://asysadmin.tumblr.com/post/17654309496/powershell-path-length-limitation

or as mentioned by AaronH answer use \?\ syntax is in this example to delete build

dir -Include build -Depth 1 | Remove-Item -Recurse -Path "\\?\$($_.FullName)"
查看更多
干净又极端
4楼-- · 2019-01-16 23:48

Combination of tools can work best, try doing a dir /x to get the 8.3 file name instead. You could then parse out that output to a text file then build a powershell script to delete the paths that you out-file'd. Take you all of a minute. Alternatively you could just rename the 8.3 file name to something shorter then delete.

查看更多
地球回转人心会变
5楼-- · 2019-01-16 23:49

This is getting old but I recently had to work around it again. I ended up using 'subst' as it didn't require any other modules or functions be available on the PC this was running from. A little more portable.

Basically find a spare drive letter, 'subst' the long path to that letter, then use that as the base for GCI.

Only limitation is that the $_.fullname and other properties will report the drive letter as the root path.

Seems to work ok:

$location = \\path\to\long\
$driveLetter = ls function:[d-z]: -n | ?{ !(test-path $_) } | random

subst $driveLetter $location
sleep 1
Push-Location $driveLetter -ErrorAction SilentlyContinue
Get-ChildItem -Recurse

subst $driveLetter /D

That command is obviously not to delete files but can be substituted.

查看更多
Viruses.
6楼-- · 2019-01-16 23:49

For my Robocopy worked in 1, 2 and 3

  1. First create an empty directory lets say c:\emptydir
  2. ROBOCOPY c:\emptydir c:\directorytodelete /purge
  3. rmdir c:\directorytodelete
查看更多
Melony?
7楼-- · 2019-01-16 23:51

Just as CADII said in another answer: Robocopy is able to create paths longer than the limit of 260 characters. Robocopy is also able to delete such paths. You can just mirror some empty folder over your path containing too long names in case you want to delete it.

For example:

robocopy C:\temp\some_empty_dir E:\temp\dir_containing_very_deep_structures /MIR

Here's the Robocopy reference to know the parameters and various options.

查看更多
登录 后发表回答