如何在Windows中找到具有大于260个字符的路径长度的文件?
-
12-12-2019 - |
题
我在XP Windows脚本中使用Xcopy来递归复制目录。我一直在获得“内存不足”错误,我理解是因为我正在尝试复制的文件有太长的路径。我可以轻松减少路径长度,但不幸的是我无法解决哪些文件违反路径长度限制。复制的文件将打印到标准输出(我将其重定向到日志文件),但错误消息被打印到终端,因此我甚至无法解决哪个目录才能给出错误。
解决方案
做一个生成扫描仪,然后在260的位置添加指南
在powershell dir /s /b > out.txt
其他提示
我也写的和博客关于一个简单的powershell脚本获取文件和目录长度。它将输出文件的长度和路径,并可选择将其写入控制台。它不会限制仅在一定长度(易于修改的情况下),但显示它们长度下降的文件,因此它仍然非常容易看出哪些路径超过您的阈值。这是:
$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()
. 作为最简单的解决方案的改进,如果您不能或不想安装PowerShell,刚刚运行:
dir /s /b | sort /r /+261 > out.txt
.
或(更快):
dir /s /b | sort /r /+261 /o out.txt
.
和长度超过260的线将到达列表的顶部。请注意,您必须添加1进行排序列参数(/ + n)。
我在此处替代使用PowerShell的其他好答案,但我还将列表保存到文件中。如果其他人需要这样的东西,将在此分享它。
警告:代码在当前工作目录中覆盖“longfilepath.txt”。我知道你有不可能的是,已经有一个,但只是在案件!
在单行中用它在一个线上想要它:
Out-File longfilepath.txt ; cmd /c "dir /b /s /a" | ForEach-Object { if ($_.length -gt 250) {$_ | Out-File -append longfilepath.txt}}
.
详细说明:
- 运行powershell
- 遍历要检查filepath长度的目录(c:works)
- 复制并粘贴代码[右键单击以粘贴powershell,或alt + space> e> p] 等待它完成,然后查看文件:
cat longfilepath.txt | sort
说明:
Out-File longfilepath.txt ;
- 创建(或覆盖)标题为“longfilepath.txt”的空白文件。半冒号分开命令。
cmd /c "dir /b /s /a" |
- 在powershell上运行dir命令,以显示包含隐藏文件的所有文件。 pipe的/a
。
|
- 对于每行(表示为$ _),如果长度大于250,请将该行附加到文件中。
从 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变量生成Generacicetagcode。
然后可以通过如此扫描变量
$AccessDenied |
Where-Object { $_.Exception -match "must be less than 260 characters" } |
ForEach-Object { $_.TargetObject }
.
如果您不关心这些文件(可能在某些情况下适用),只需删除AccessDenied
部分。
tlpd(“太长路径目录”)是节省了我的程序。 非常易于使用:
对于大于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}
.
屏幕截图:
用于大于10的文件名:
Get-ChildItem | Where-Object {$_.PSChildName.Length -gt 10}
.
屏幕截图: