我有一个将从第三方应用程序动态生成的字符串
$somePath = "D:\some\path\name.of - my file [20_32_21].mp4"
我需要能够在函数中验证此路径。
$somePath = "D:\some\path\name.of - my file [20_32_21].mp4"
Function ValidatePath{
Param($path)
if(Test-Path $path){
Write-Host "Worked"
} else {
Write-Host "Didn't Work"
}
}
ValidatePath $somePath
# DIDN'T WORK
问题是它在方括号上失败了。
如何自动转义方括号以验证文件?
# Path needs to look like this
$somePath = "D:\some\path\name.of - my file ``[20_32_21``].mp4"
ValidatePath $somePath
# WORKED!!!
答案 0 :(得分:5)
使用-LiteralPath代替-Path; e.g:
if ( test-path -literalpath $path ) {
....
}
比尔
答案 1 :(得分:0)
您可以尝试使用“test-path -literalpath $ path”吗?