获取文件夹及其子文件夹中最长文件路径的长度

时间:2017-01-18 17:55:55

标签: windows powershell batch-file

我正在寻找一个可以从命令行(batch \ PowerShell)运行的脚本,该脚本将遍历文件夹及其子文件夹,并返回一个长度为最长文件路径的数字。

我已经看到了一些批处理和PowerShell脚本,比如
How do I find files with a path length greater than 260 characters in Windows?
但它们都不能满足我的要求。

请注意,文件路径可能超过256个字符

1 个答案:

答案 0 :(得分:3)

的PowerShell:

((Get-ChildItem -Recurse).FullName  | Measure-Object -Property Length -Maximum).Maximum

命令行:

powershell -exec Bypass -c "((dir -rec).FullName | measure Length -max).Maximum"

修改

错误相关的

Get-ChildItem : The specified path, file name, or both are too long :阅读Maximum Path Length Limitation和相关的[PowerShell]-tagged StackOverflow threads

PS D:\PShell> ((Get-ChildItem "D:\odds and ends" -Directory -Recurse).FullName  | Measure-Object -Property Length -Maximum).Maximum
242

PS D:\PShell> ((Get-ChildItem "D:\odds and ends" -Recurse -ErrorAction SilentlyContinue).FullName  | Measure-Object -Property Length -Maximum).Maximum
242

请注意,上述命令中的-ErrorAction SilentlyContinue仅禁止显示错误消息。但是,我知道后面的242值返回错误。

我的工作区应用cmd /C dir /B /S代替(Get-ChildItem -Recurse).FullName,如下所示:

PS D:\PShell> $x = (. cmd /C dir /B /S "D:\odds and ends")
PS D:\PShell> $y = ( $x | Measure-Object -Property Length -Maximum).Maximum
PS D:\PShell> $y
273
PS D:\PShell> $z = $x | Where-Object { $_.Length -gt 260 }
PS D:\PShell> $z.GetTypeCode()
String
PS D:\PShell> $z
D:\odds and ends\ZalohaGogen\WDElements\zalohaeva\zaloha_honza\Music\Jazz\!Kompilace\Saint Germain des Pres Cafe Vol. 1 to 8 - The Finest Electro Jazz Complication\Saint Germain Des Pres Cafe Vol. 7 - The Finest Electro Jazz Complication\CD 1\Configuring and Using Inte.txt
PS D:\PShell>