powershell - 如何让 Get-ChildItem 处理带有不间断空格的路径

标签 powershell unicode character-encoding filesystems get-childitem

我有以下适用于大多数文件的代码。输入文件 (FoundLinks.csv) 是一个 UTF-8 文件,每行一个文件路径。它是我需要处理的特定驱动器上文件的完整路径。

$inFiles = @()
$inFiles += @(Get-Content -Path "C:\Users\sw_admin\FoundLinks.csv")

foreach ($inFile in $inFiles) {
    Write-Host("Processing: " + $inFile)
    $objFile = Get-ChildItem -LiteralPath $inFile
    New-Object PSObject -Prop @{ 
        FullName = $objFile.FullName
        ModifyTime = $objFile.LastWriteTime
    }
} 

但即使我使用了 -LiteralPath,它仍然无法处理文件名中包含不间断空格的文件。

Processing: q:\Executive\CLC\Budget\Co  2018 Budget - TO Bob (GA Prophix).xlsx
Get-ChildItem : Cannot find path 'Q:\Executive\CLC\Budget\Co  2018 Budget - TO Bob (GA Prophix).xlsx'
because it does not exist.
At ListFilesWithModifyTime.ps1:6 char:29
+     $objFile = Get-ChildItem <<<<  -LiteralPath $inFile
    + CategoryInfo          : ObjectNotFound: (Q:\Executive\CL...A Prophix).xlsx:String) [Get-ChildItem], ItemNotFound
   Exception
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

我知道我的输入文件的路径中有不间断空格,因为我可以在记事本中打开它,复制有问题的路径,粘贴到 Word 中,然后打开段落标记。它显示了 2018 年之前的正常空间,后跟 NBSP。

PowerShell 没有读取 NBSP 中的内容吗?我将它错误地传递给 -LiteralPath 吗?我已经无计可施了。我看到了 this solution ,但在这种情况下,他们在脚本中将路径作为文字提供,所以我不知道如何使用这种方法。

我也尝试过:Get-Content 上的 -Encoding UTF8 参数,但没有区别。

我什至不确定如何检查代码中的 $inFile 以确认它是否仍包含 NBSP。

感谢任何帮助摆脱困境的帮助!

确认$inFile有NBSP

谢谢大家!根据@TheMadTechnician,我已经更新了这样的代码,并将我的输入文件减少到只有一个有问题的文件。

$inFiles = @()
$inFiles += @(Get-Content -Path "C:\Users\sw_admin\FoundLinks.csv" -Encoding UTF8)

foreach ($inFile in $inFiles) {
    Write-Host("Processing: " + $inFile)

    # list out all chars to confirm it has an NBSP
    $inFile.ToCharArray()|%{"{0} -> {1}" -f $_,[int]$_}

    $objFile = Get-ChildItem -LiteralPath $inFile
    New-Object PSObject -Prop @{ 
        FullName = $objFile.FullName
        ModifyTime = $objFile.LastWriteTime
    }
} 

现在我可以确认 $inFile 实际上仍然包含 NBSP,就像它传递给 Get-ChildItem 一样。然而 Get-ChildItem 说该文件不存在。

我尝试过的更多:

  • 如果我使用 Get-Item 而不是 Get-ChildItem,则相同
  • 如果我使用 -Path 而不是 -LiteralPath,则相同
  • Windows 资源管理器和 Excel 可以成功处理该文件。

我使用的是 Windows 7 计算机、Powershell 2。

再次感谢大家的回复!

最佳答案

目前还不清楚为什么 Sandra 的代码不起作用:PowerShell v2+ 能够检索路径包含非 ASCII 字符的文件;也许涉及到具有不同字符编码的非 NTFS 文件系统?

但是,以下解决方法被证明是有效的:

$objFile = Get-ChildItem -Path ($inFile -replace ([char] 0xa0), '?')
  • 这个想法是替换不间断空格字符。 (Unicode U+00A0 ;十六进制。0xa )在输入文件路径中带有通配符 ? ,代表任何单个字符

  • 对于 Get-ChildItem执行通配符匹配,-Path而不是-LiteralPath必须使用(请注意,如果您按位置传递路径参数作为第一个参数,则 -Path 实际上是默认值)。

  • 假设,基于通配符的路径可以匹配多个文件;如果是这种情况,则必须检查各个匹配项,以确定在 ? 位置具有不间断空格的特定匹配项。 .

关于powershell - 如何让 Get-ChildItem 处理带有不间断空格的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50731177/

相关文章:

powershell - Powershell 中的 RegKey 问题

powershell哈希表问题

java - 在 Java 中打印数学符号给出错误的输出

character-encoding - MySQL - 更新时无效的 utf8mb4 字符串

java - Android TextView 显示音标(IPA)符号?

PHP preg_split Apache 和 Powershell 等价物

powershell - 为什么这个函数返回 ''而不是连接字符串

python - 为什么在 python -c 中插入 unicode 字符抛出异常

python - BeautifulSoup4 stripped_strings 给我字节对象?

c++ - 如何防止非 Unicode 应用程序在将资源加载到不同本地化的机器上时转换资源的字符集?