gpt4 book ai didi

powershell - 使用长文件路径时的Get-ChildItem错误处理

转载 作者:行者123 更新时间:2023-12-03 08:51:36 25 4
gpt4 key购买 nike

我正在尝试通过文件夹扫描时处理错误。假设我有类似的东西:

Get-ChildItem $somepath -Directory | ForEach-Object {
if(error occurred due to too long path) {
skip this folder then
} else {
Write-Host $_.BaseName
}
}

当我这样做时,我会以 $somepath打印文件夹,直到其中一个文件夹太长,然后循环停止。即使使用 SilentlyContinue。即使到达太长的文件夹,我也想打印。

最佳答案

如果您可以安装非较旧的PowerShell版本(3.0或更高版本),只需在路径前添加\\?\即可克服完整路径的260个字符的限制:

Get-ChildItem "\\?\$somepath" | ForEach {
# ............
}

关于powershell - 使用长文件路径时的Get-ChildItem错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39572006/

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