【发布时间】:2022-01-19 14:05:48
【问题描述】:
我编写了一个 powershell 脚本来从一组指定的根路径中的所有文件中删除 R/H/S 属性。相关代码为:
$Mask = [System.IO.FileAttributes]::ReadOnly.Value__ -bor [System.IO.FileAttributes]::Hidden.Value__ -bor [System.IO.FileAttributes]::System.Value__
Get-ChildItem -Path $Paths -Force -Recurse -ErrorAction SilentlyContinue | ForEach-Object {
$Value = $_.Attributes.value__
if($Value -band $Mask) {
$Value = $Value -band -bnot $Mask
if($PSCmdlet.ShouldProcess($_.FullName, "Set $([System.IO.FileAttributes] $Value)")) {
$_.Attributes = $Value
}
}
}
这很好用,但是在处理一个非常大的文件夹结构时,我遇到了一些这样的错误:
Exception setting "Attributes": "Could not find a part of the path 'XXXXXXXXXX'."
At YYYYYYYYYY\Grant-FullAccess.ps1:77 char:17
+ $_.Attributes = $Value
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
我觉得这很奇怪,因为被操作的 FileInfo 对象肯定存在,因为它来自文件搜索。
我不能提供文件名,因为它们是机密的,但我可以说:
- 它们的长度为 113-116 个字符
- 所涉及的唯一字符集是
%()+-.0123456789ABCDEFGIKLNOPRSTUVWX,在文件名中没有一个是非法的 -
%字符由于 URL 编码的空格 (%20) 而存在
您对造成这种情况的原因有什么建议吗?我假设如果完整路径太长,或者我没有对该文件的写入权限,则会引发更合适的错误。
【问题讨论】:
-
这些是 UNC 路径 (\\server\share\restofpath) 还是本地路径?
-
@mklement0:我的目的是处理目录和文件,但感谢您的提示。
-
@mklement0:关于长路径前缀,我现在怀疑这是一个长路径问题。我今天会试试。
-
@Theo:路径将始终位于本地驱动器上。
标签: powershell