TTCM
TTCM

Reputation: 55

How can I determine the specific character file with powershell?

I have a file.

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

but powershell test-path error.

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

Does anyone have some ideas on how to go about this? Thanks!

Upvotes: 0

Views: 43

Answers (1)

vonPryz
vonPryz

Reputation: 24081

First off, having such file names is begging for trouble. They are much more trouble than help.

That being said, Powershell quoting rules explains how to work with quotes. Since there is a single quote in the file name, it must be escaped by doubling it - the usual backtick doesn't help here. Here-Strings work too. Like so,

# 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

Upvotes: 1

Related Questions