Delete directory regardless of 260 char limit

后端 未结 17 1630
無奈伤痛
無奈伤痛 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条回答
  • 2020-12-08 02:35

    There is one workaround that uses Experimental.IO from Base Class Libraries project. You can find it over on poshcode, or download from author's blog. 260 limitation is derived from .NET, so it's either this, or using tools that do not depend on .NET (like cmd /c dir, as @Bill suggested).

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

    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.

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

    This answer on SuperUser solved it for me: https://superuser.com/a/274224/85532

    Cmd /C "rmdir /S /Q $myDir"
    
    0 讨论(0)
  • 2020-12-08 02:38

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

    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)

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

    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

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