如何在 Windows 中查找路径长度大于 260 个字符的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12697259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I find files with a path length greater than 260 characters in Windows?
提问by WestHamster
I'm using a xcopy in an XP windows script to recursively copy a directory. I keep getting an 'Insufficient Memory' error, which I understand is because a file I'm trying to copy has too long a path. I can easily reduce the path length, but unfortunately I can't work out which files are violating the path length restriction. The files that are copied are printed to the standard output (which I'm redirecting to a log file), but the error message is printed to the terminal, so I can't even work out approximately which directory the error is being given for.
我在 XP Windows 脚本中使用 xcopy 递归复制目录。我不断收到“内存不足”错误,我理解这是因为我尝试复制的文件路径太长。我可以轻松地减少路径长度,但不幸的是我无法确定哪些文件违反了路径长度限制。复制的文件打印到标准输出(我将其重定向到日志文件),但错误消息打印到终端,所以我什至无法确定错误是针对哪个目录给出的.
回答by rerun
do a dir /s /b > out.txt
and then add a guide at position 260
做一个dir /s /b > out.txt
,然后在位置 260 添加一个指南
In powershell cmd /c dir /s /b |? {$_.length -gt 260}
在 PowerShell 中 cmd /c dir /s /b |? {$_.length -gt 260}
回答by deadlydog
I created the Path Length Checker toolfor this purpose, which is a nice, free GUI app that you can use to see the path lengths of all files and directories in a given directory.
为此,我创建了 Path Length Checker 工具,这是一个不错的免费 GUI 应用程序,您可以使用它来查看给定目录中所有文件和目录的路径长度。
I've also written and blogged about a simple PowerShell scriptfor getting file and directory lengths. It will output the length and path to a file, and optionally write it to the console as well. It doesn't limit to displaying files that are only over a certain length (an easy modification to make), but displays them descending by length, so it's still super easy to see which paths are over your threshold. Here it is:
我还写过一篇关于获取文件和目录长度的简单 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()
回答by Chungalin
As a refinement of simplest solution, and if you can't or don't want to install Powershell, just run:
作为最简单解决方案的改进,如果您不能或不想安装 Powershell,只需运行:
dir /s /b | sort /r /+261 > out.txt
or (faster):
或(更快):
dir /s /b | sort /r /+261 /o out.txt
And lines longer than 260 will get to the top of listing. Note that you must add 1 to SORT column parameter (/+n).
长度超过 260 的行将到达列表顶部。请注意,您必须将 1 添加到 SORT 列参数 (/+n)。
回答by Rani Kheir
I've made an alternative to the other good answers on here that uses PowerShell, but mine also saves the list to a file. Will share it here in case anyone else needs wants something like that.
我已经替代了此处使用 PowerShell 的其他好答案,但我的也将列表保存到文件中。将在这里分享,以防其他人需要类似的东西。
Warning:Code overwrites "longfilepath.txt" in the current working directory. I know it's unlikely you'd have one already, but just in case!
警告:代码覆盖当前工作目录中的“longfilepath.txt”。我知道您不太可能已经拥有一个,但以防万一!
Purposely wanted it in a single line:
特意将它放在一行中:
Out-File longfilepath.txt ; cmd /c "dir /b /s /a" | ForEach-Object { if ($_.length -gt 250) {$_ | Out-File -append longfilepath.txt}}
Detailed instructions:
详细说明:
- Run PowerShell
- Traverse to the directory you want to check for filepath lengths (C: works)
- Copy and paste the code [Right click to paste in PowerShell, or Alt + Space > E > P]
- Wait until it's done and then view the file:
cat longfilepath.txt | sort
- 运行 PowerShell
- 遍历到要检查文件路径长度的目录(C:works)
- 复制并粘贴代码 [右键单击以在 PowerShell 中粘贴,或 Alt + Space > E > P]
- 等到它完成,然后查看文件:
cat longfilepath.txt | sort
Explanation:
解释:
Out-File longfilepath.txt ;
– Create (or overwrite) a blank file titled 'longfilepath.txt'. Semi-colon to separate commands.
Out-File longfilepath.txt ;
– 创建(或覆盖)一个名为“longfilepath.txt”的空白文件。分号分隔命令。
cmd /c "dir /b /s /a" |
– Run dir command on PowerShell, /a
to show all files including hidden files. |
to pipe.
cmd /c "dir /b /s /a" |
– 在 PowerShell 上运行 dir 命令,/a
以显示包括隐藏文件在内的所有文件。|
管。
ForEach-Object { if ($_.length -gt 250) {$_ | Out-File -append longfilepath.txt}}
– For each line (denoted as $_), if the length is greater than 250, append that line to the file.
ForEach-Object { if ($_.length -gt 250) {$_ | Out-File -append longfilepath.txt}}
– 对于每一行(表示为 $_),如果长度大于 250,则将该行附加到文件中。
回答by pollaris
TLPD ("too long path directory") is the program that saved me. Very easy to use:
TLPD(“太长的路径目录”)是拯救我的程序。非常容易使用:
回答by Jonno
From http://www.powershellmagazine.com/2012/07/24/jaap-brassers-favorite-powershell-tips-and-tricks/:
从http://www.powershellmagazine.com/2012/07/24/jaap-brassers-favorite-powershell-tips-and-tricks/:
Get-ChildItem –Force –Recurse –ErrorAction SilentlyContinue –ErrorVariable AccessDenied
the first part just iterates through this and sub-folders; using -ErrorVariable AccessDenied
means push the offending items into the powershell variable AccessDenied
.
第一部分只是遍历这个文件夹和子文件夹;using-ErrorVariable AccessDenied
意味着将有问题的项目推送到 powershell 变量中AccessDenied
。
You can then scan through the variable like so
然后您可以像这样扫描变量
$AccessDenied |
Where-Object { $_.Exception -match "must be less than 260 characters" } |
ForEach-Object { $_.TargetObject }
If you don't care about these files (may be applicable in some cases), simply drop the -ErrorVariable AccessDenied
part.
如果您不关心这些文件(在某些情况下可能适用),只需删除该-ErrorVariable AccessDenied
部分即可。
回答by SeanC
you can redirect stderr.
你可以重定向标准错误。
more explanation here, but having a command like:
更多解释here,但有一个命令,如:
MyCommand >log.txt 2>errors.txt
should grab the data you are looking for.
应该抓取您正在寻找的数据。
Also, as a trick, Windows bypasses that limitation if the path is prefixed with \\?\
(msdn)
此外,作为一个技巧,如果路径以\\?\
( msdn)为前缀,Windows 会绕过该限制
Another trick if you have a root or destination that starts with a long path, perhaps SUBST
will help:
如果您有一个以长路径开头的根或目的地,另一个技巧可能SUBST
会有所帮助:
SUBST Q: "C:\Documents and Settings\MyLoginName\My Documents\MyStuffToBeCopied"
Xcopy Q:\ "d:\Where it needs to go" /s /e
SUBST Q: /D
回答by E235
For paths greater than 260:
you can use:
对于大于 260 的路径:
您可以使用:
Get-ChildItem | Where-Object {$_.FullName.Length -gt 260}
Example on 14 chars:
To view the paths lengths:
14 个字符的示例:
查看路径长度:
Get-ChildItem | Select-Object -Property FullName, @{Name="FullNameLength";Expression={($_.FullName.Length)}
Get paths greater than 14:
获取大于 14 的路径:
Get-ChildItem | Where-Object {$_.FullName.Length -gt 14}
Screenshot:
截屏:
For filenames greater than 10:
对于大于 10 的文件名:
Get-ChildItem | Where-Object {$_.PSChildName.Length -gt 10}
Screenshot:
截屏: