错误“找不到路径的一部分"在现有文件上设置属性时 [英] Error "Could not find a part of the path" while setting attributes on an existing file

查看:55
本文介绍了错误“找不到路径的一部分"在现有文件上设置属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 powershell 脚本来从一组指定的根路径中的所有文件中去除 R/H/S 属性.相关代码为:

I wrote a powershell script to strip R/H/S attributes off all files in a specified set of root paths. The relevant code is:

$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
        }
    }
}

这很好用,但是在处理一个非常大的文件夹结构时,我遇到了一些这样的错误:

This works fine, but when processing one very large folder structure, I got a few errors like this:

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 对象肯定存在,因为它来自文件搜索.

I find this strange because the FileInfo object being manipulated is guaranteed to exist, since it comes from a file search.

我不能提供文件名,因为它们是机密的,但我可以说:

I can't give the file names because they are confidential, but I can say:

  • 它们的长度为 113-116 个字符
  • 所涉及的唯一字符集是%()+-.0123456789ABCDEFGIKLNOPRSTUVWX,在文件名中没有一个是非法的
  • 由于 URL 编码的空格 (%20) 存在 % 字符
  • they are 113-116 characters long
  • the unique set of characters involved are %()+-.0123456789ABCDEFGIKLNOPRSTUVWX, none of which are illegal in a file name
  • the % character is there due to URL-encoded spaces (%20)

对于可能导致这种情况的原因,您有什么建议吗?我假设如果完整路径太长,或者我没有文件的写权限,那么会抛出一个更合适的错误.

Do you have any suggestions as to what may be causing this? I assume that if the full path was too long, or I didn't have write permissions to the file, then a more appropriate error would be thrown.

推荐答案

您自己的答案所述,问题原来是一条过长的路径(长于 259 个字符的传统限制.)

As stated in your own answer, the problem turned out to be an overly long path (longer than the legacy limit of 259 chars.)

除了通过组策略启用长路径支持之外,您还可以通过注册表在每台计算机的基础上启用它em> 如下,需要以海拔(以管理员身份)运行:

In addition to enabling long-path support via Group Policy, you can enable it on a per-computer basis via the registry as follows, which requires running with elevation (as admin):

# NOTE: Must be run elevated (as admin).
# Change will take effect in FUTURE sessions.
Set-ItemProperty HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem LongPathsEnabled 1

通过 0 关闭支持.

Pass 0 to turn support off.

但是,即使关闭支持长路径(Windows 10 之前的版本总是如此),也可以处理长路径:

However, even with long-path supported turned OFF (as is invariably the case on pre-Windows 10 versions) it is possible to handle long paths:

  • Windows PowerShell(PowerShell 最高版本 5.1)中,您必须使用长路径选择加入前缀em>, \\?\,如下所述.

  • In Windows PowerShell (PowerShell up to version 5.1), you must use the long-path opt-in prefix, \\?\, as discussed below.

PowerShell [Core] v6+ 中,不需要额外的工作,因为它总是支持长路径 - 你不需要要在系统范围内启用支持,也不需要下面讨论的长路径前缀.

In PowerShell [Core] v6+, no extra work is needed, because it always supports long paths - you neither need to turn on support system-wide nor do you need the long-path prefix discussed below.

  • 警告:虽然原则上您可以在 PowerShell [Core] 中使用 \\?\,但对它的支持不一致,因为v7.0.0-rc.2 的;请参阅此 GitHub 问题.
  • Caveat: While you may use \\?\ in PowerShell [Core] as well in principle, support for it is inconsistent as of v7.0.0-rc.2; see this GitHub issue.

重要:前缀 \\?\ 仅适用于以下条件:

Important: Prefix \\?\ only works under the following conditions:

  • 前缀路径必须是完整(绝对)、规范化路径(不得包含... 组件).

  • The prefixed path must be a full (absolute), normalized path (must not contain . or .. components).

  • 例如,\\?\C:\path\to\foo.txt 有效,但 \\?\.\foo.txt 无效.立>
  • 此外,如果路径是 UNC 路径,则路径需要不同的形式:
    • \\?\UNC\\\...;
    • 例如,\\server1\share2 必须表示为 \\?\UNC\server1\share2
    • E.g., \\?\C:\path\to\foo.txt works, but \\?\.\foo.txt does not.
    • Furthermore, if the path is a UNC path, the path requires a different form:
      • \\?\UNC\<server>\<share>\...;
      • E.g., \\server1\share2 must be represented as \\?\UNC\server1\share2

      这篇关于错误“找不到路径的一部分"在现有文件上设置属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆