无论260 char限制,都删除目录

mho*_*100 45 powershell limit

我写了一个简单的脚本,在一定天后删除USMT迁移文件夹:

## 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
}
Run Code Online (Sandbox Code Playgroud)

但是我一直在畏惧 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.

我一直在寻找解决方法和替代方案,但它们都围绕着我关心文件夹中的内容.

我希望有一个更简单的解决方案,因为如果标记为删除,我并不真正关心文件夹内容.

是否有除Remove-Item -recurse之外的任何本机Powershell cmdlet 可以完成我之后的操作?

And*_*ndy 56

我经常遇到节点项目的这个问题.它们嵌套它们的依赖关系,一旦git克隆,就很难删除它们.我遇到的一个很好的节点实用程序是rimraf.

npm install rimraf -g
rimraf <dir>
Run Code Online (Sandbox Code Playgroud)

  • 除了使用RIMRAF之外别无其他功能.万分感谢:) (3认同)
  • 伟大的!robocopy 解决方案部分工作,它删除了一些文件,但不是所有文件。这个 rimraf 工作完美。非常感谢@Andy! (2认同)

小智 40

正如CADII在另一个答案中所说:Robocopy能够创建超过260个字符的路径.Robocopy还能够删除这些路径.您可以在路径上镜像一些包含太长名称的空文件夹,以防您想删除它.

例如:

robocopy C:\temp\some_empty_dir E:\temp\dir_containing_very_deep_structures /MIR
Run Code Online (Sandbox Code Playgroud)

以下是了解参数和各种选项的Robocopy参考.


Dan*_*Lee 22

SuperUser上的这个答案为我解决了这个问题:https://superuser.com/a/274224/85532

Cmd /C "rmdir /S /Q $myDir"
Run Code Online (Sandbox Code Playgroud)

  • 遗憾的是,这并不总是奏效. (9认同)

Mar*_*ndl 15

我创建了一个PowerShell函数,它能够使用上面提到的robocopy技术删除长路径(> 260):

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
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

Remove-PathToLongDirectory c:\yourlongPath
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:在版本号为 14393(周年更新)的 Windows 10 中,.NET 中的路径限制错误已得到修复。您需要在本地策略中启用长路径支持,并且您可以使用长路径做任何您想做的事情,而不必求助于 P/Invoke 或 robocopy (2认同)
  • 这就像一场梦。有几个月我用subst z:myLongPath (2认同)

小智 11

我不久前学会了一个技巧,通常可以解决长文件路径问题.显然,当使用某些Windows API时,某些功能将流经无法处理长文件名的遗留代码.但是,如果以特定方式格式化路径,则可以避免遗留代码.解决此问题的技巧是使用"\\?\"前缀引用路径.应该注意的是,并非所有API都支持此功能,但在这种特殊情况下它对我有用,请参阅下面的示例:

以下示例失败:

PS D:\> get-childitem -path "D:\System Volume Information\dfsr" -hidden


Directory: D:\System Volume Information\dfsr


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a-hs        10/09/2014  11:10 PM     834424 FileIDTable_2
-a-hs        10/09/2014   8:43 PM    3211264 SimilarityTable_2

PS D:\> Remove-Item -Path "D:\System Volume Information\dfsr" -recurse -force
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.
At line:1 char:1
+ Remove-Item -Path "D:\System Volume Information\dfsr" -recurse -force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : WriteError: (D:\System Volume Information\dfsr:String) [Remove-Item], PathTooLongExcepti
on
+ FullyQualifiedErrorId : RemoveItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand

PS D:\>
Run Code Online (Sandbox Code Playgroud)

但是,在路径前加上"\\?\"会使命令成功运行:

PS D:\> Remove-Item -Path "\\?\D:\System Volume Information\dfsr" -recurse -force
PS D:\> get-childitem -path "D:\System Volume Information\dfsr" -hidden
PS D:\>
Run Code Online (Sandbox Code Playgroud)

  • 我没有错误,但它也没有删除该文件夹. (5认同)
  • 这对我有用的前缀,赢得 10 powershell 5.1。像这样 Remove-Item -Recurse -Force -Path "\\?\$($_.FullName)" (2认同)

Nic*_*Dao 10

如果你安装了ruby,你可以使用Fileman:

gem install fileman
Run Code Online (Sandbox Code Playgroud)

安装后,您只需在命令提示符中运行以下命令:

fm rm your_folder_path
Run Code Online (Sandbox Code Playgroud)

当你在Windows上使用node.js进行开发时,这个问题真的很痛苦,所以fileman在一段时间内删除所有垃圾变得非常方便


Bil*_*ill 9

这是一个已知的限制PowerShell.解决方法是使用dircmd(对不起,但这是真的).

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

或者如AaronH所提到的那样回答使用\?\语法在这个例子中删除构建

dir -Include build -Depth 1 | Remove-Item -Recurse -Path "\\?\$($_.FullName)"
Run Code Online (Sandbox Code Playgroud)

  • "dir"似乎也失败了. (4认同)

小智 5

如果你所做的只是删除文件,我使用一个函数来缩短名称,然后我删除.

    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++
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道这是一个老问题,但我想我会把它放在这里,万一有人需要它.