Delete directory regardless of 260 char limit

后端 未结 17 1631
無奈伤痛
無奈伤痛 2020-12-08 02:03

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

## Server List ##
$servers = \"Delorean\",\"Adelaide\",\"Brisba         


        
相关标签:
17条回答
  • 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.

    0 讨论(0)
  • 2020-12-08 02:44

    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)"
    
    0 讨论(0)
  • 2020-12-08 02:46

    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
    0 讨论(0)
  • 2020-12-08 02:46

    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

    0 讨论(0)
  • 2020-12-08 02:48

    PowerShell can easily be used with AlphaFS.dll to do actual file I/O stuff without the PATH TOO LONG hassle.

    For example:

    Import-Module <path-to-AlphaFS.dll>
    
    [Alphaleonis.Win32.Filesystem.Directory]::Delete($path, $True)
    

    Please see at Codeplex: https://alphafs.codeplex.com/ for this .NET project.

    0 讨论(0)
提交回复
热议问题