gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:22:26 25 4
gpt4 key购买 nike

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

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

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

最佳答案

PowerShell:

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

命令行:

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

编辑

错误相关:Get-ChildItem:指定的路径、文件名或两者都太长:阅读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>

关于windows - 获取文件夹及其子文件夹中最长文件路径的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41726248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com