首页 > 解决方案 > 如何使用 powershell 确定特定的字符文件?

问题描述

我有一个文件。

c:\\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx

但powershell测试路径错误。

PS C:\\> Test-Path -LiteralPath "C:\\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx" -PathType Leaf

PS C:\\> False

PS C:\\> Test-Path "C:\\[test^!#$%&'()=~{`}_+-^[];.,] test.xlsx" -PathType Leaf

PS C:\\> False

有人对如何解决这个问题有一些想法吗?谢谢!

标签: powershell

解决方案


首先,拥有这样的文件名是自找麻烦。他们带来的麻烦多于帮助。

话虽如此,Powershell引用规则解释了如何使用引号。由于文件名中有一个单引号,因此必须通过加倍来对其进行转义 - 通常的反引号在这里没有帮助。Here-Strings 也可以。像这样,

# single quote twice
test-path -literalpath '[test^!#$%&''()=~{`}_+-^[];.,] test.xlsx'
True

# here-string
test-path -literalpath @'
>> [test^!#$%&'()=~{`}_+-^[];.,] test.xlsx
>> '@
True

# here-string in varialbe
$f =@'
>> [test^!#$%&'()=~{`}_+-^[];.,] test.xlsx
>> '@

test-path -literalpath $f
True

推荐阅读