使用长文件路径时的 Get-ChildItem 错误处理 [英] Get-ChildItem error handling when using long file paths
本文介绍了使用长文件路径时的 Get-ChildItem 错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试处理扫描文件夹时出现的错误.假设我有类似的东西:
I am trying to handle errors when scanning through folders. Let's say I have something like:
Get-ChildItem $somepath -Directory | ForEach-Object {
if(error occurred due to too long path) {
skip this folder then
} else {
Write-Host $_.BaseName
}
}
当我这样做时,我会打印 $somepath
中的文件夹,直到其中一个太长,然后循环停止.即使使用 SilentlyContinue
.即使到达太长的文件夹,我也想打印.
When I do this I print the folders in $somepath
until one of them is too long and then the loop stops. Even when using SilentlyContinue
. I want to print even after reaching a folder that is too long.
推荐答案
如果您可以安装非古老的 PowerShell 版本(3.0 或更高版本),只需在路径前加上 \\?\
即可克服完整路径的 260 个字符限制:
If you can install a non-ancient PowerShell version (3.0 or newer), simply prepend the path with \\?\
to overcome the 260-character limit for full path:
Get-ChildItem "\\?\$somepath" | ForEach {
# ............
}
这篇关于使用长文件路径时的 Get-ChildItem 错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文