如何在Windows中查找路径长度超过260个字符的文件?

时间:2012-10-02 19:51:40

标签: windows windows-xp xcopy max-path

我在XP Windows脚本中使用xcopy来递归复制目录。我不断收到“Insufficient Memory”错误,我理解这是因为我试图复制的文件路径太长了。我可以轻松地减少路径长度,但不幸的是我无法确定哪些文件违反了路径长度限制。复制的文件将打印到标准输出(我将其重定向到日志文件),但错误消息将打印到终端,因此我甚至无法计算出哪个目录正在给出错误

8 个答案:

答案 0 :(得分:96)

执行dir /s /b > out.txt,然后在260位添加指南

在powershell cmd /c dir /s /b |? {$_.length -gt 260}

答案 1 :(得分:28)

我为此创建了the Path Length Checker tool,这是一个很好的免费GUI应用程序,可以用来查看给定目录中所有文件和目录的路径长度。

我还written and blogged about a simple PowerShell script获取文件和目录长度。它将输出文件的长度和路径,并可选择将其写入控制台。它不限制显示仅超过一定长度的文件(一个简单的修改),但显示它们按长度递减,因此仍然可以非常容易地看到哪些路径超过了您的阈值。这是:

$pathToScan = "C:\Some Folder"  # The path to scan and the the lengths for (sub-directories will be scanned as well).
$outputFilePath = "C:\temp\PathLengths.txt" # This must be a file in a directory that exists and does not require admin rights to write to.
$writeToConsoleAsWell = $true   # Writing to the console will be much slower.

# Open a new file stream (nice and fast) and write all the paths and their lengths to it.
$outputFileDirectory = Split-Path $outputFilePath -Parent
if (!(Test-Path $outputFileDirectory)) { New-Item $outputFileDirectory -ItemType Directory }
$stream = New-Object System.IO.StreamWriter($outputFilePath, $false)
Get-ChildItem -Path $pathToScan -Recurse -Force | Select-Object -Property FullName, @{Name="FullNameLength";Expression={($_.FullName.Length)}} | Sort-Object -Property FullNameLength -Descending | ForEach-Object {
    $filePath = $_.FullName
    $length = $_.FullNameLength
    $string = "$length : $filePath"

    # Write to the Console.
    if ($writeToConsoleAsWell) { Write-Host $string }

    #Write to the file.
    $stream.WriteLine($string)
}
$stream.Close()

答案 2 :(得分:22)

作为最简单解决方案的改进,如果您不能或不想安装Powershell,只需运行:

dir /s /b | sort /r /+261 > out.txt

或(更快):

dir /s /b | sort /r /+261 /o out.txt

超过260的行将成为列表的顶部。请注意,必须将1添加到SORT列参数(/ + n)。

答案 3 :(得分:3)

我在这里使用PowerShell替代了其他好的答案,但我也将列表保存到文件中。如果其他人需要这样的东西,请在这里分享。

警告:代码覆盖" longfilepath.txt"在当前的工作目录中。我知道你不太可能已经拥有一个,但以防万一!

有意想把它放在一行:

Out-File longfilepath.txt ; cmd /c "dir /b /s /a" | ForEach-Object { if ($_.length -gt 250) {$_ | Out-File -append longfilepath.txt}}

详细说明:

  1. 运行PowerShell
  2. 遍历到要检查文件路径长度的目录(C:工作)
  3. 复制并粘贴代码[右键单击以粘贴PowerShell,或Alt + Space> E> P]
  4. 等到它完成后再查看文件:cat longfilepath.txt | sort
  5. <强>解释

    Out-File longfilepath.txt ; - 创建(或覆盖)标题为&#39; longfilepath.txt&#39;的空白文件。用分号分隔命令。

    cmd /c "dir /b /s /a" | - 在PowerShell上运行dir命令,/a以显示包括隐藏文件在内的所有文件。管道|

    ForEach-Object { if ($_.length -gt 250) {$_ | Out-File -append longfilepath.txt}} - 对于每一行(表示为$ _),如果长度大于250,则将该行附加到文件中。

答案 4 :(得分:2)

你可以重定向stderr。

更多解释here,但有一个如下命令:

MyCommand >log.txt 2>errors.txt

应该抓住你想要的数据。

另外,作为一种技巧,如果路径以\\?\为前缀(msdn

,Windows会绕过该限制

如果你有一个以长路径开头的根或目的地的另一个技巧,可能SUBST会有所帮助:

SUBST Q: "C:\Documents and Settings\MyLoginName\My Documents\MyStuffToBeCopied"
Xcopy Q:\ "d:\Where it needs to go" /s /e
SUBST Q: /D

答案 5 :(得分:2)

来自http://www.powershellmagazine.com/2012/07/24/jaap-brassers-favorite-powershell-tips-and-tricks/

Get-ChildItem –Force –Recurse –ErrorAction SilentlyContinue –ErrorVariable AccessDenied

第一部分只是迭代这个和子文件夹;使用-ErrorVariable AccessDenied表示将违规项目推送到powershell变量AccessDenied

然后你可以像这样扫描变量

$AccessDenied |
Where-Object { $_.Exception -match "must be less than 260 characters" } |
ForEach-Object { $_.TargetObject }

如果您不关心这些文件(在某些情况下可能适用),只需删除-ErrorVariable AccessDenied部分。

答案 6 :(得分:1)

TLPD(“路径目录太长”)是保存我的程序。 非常易于使用:

https://sourceforge.net/projects/tlpd/

答案 7 :(得分:-1)

对于大于260的路径:
你可以使用:

Get-ChildItem | Where-Object {$_.FullName.Length -gt 260}

14个字符的示例:
要查看路径长度:

Get-ChildItem | Select-Object -Property FullName, @{Name="FullNameLength";Expression={($_.FullName.Length)}

获取大于14的路径:

Get-ChildItem | Where-Object {$_.FullName.Length -gt 14}  

截图:
enter image description here

对于大于10的文件名:

Get-ChildItem | Where-Object {$_.PSChildName.Length -gt 10}

截图:
enter image description here

相关问题