【发布时间】:2012-04-26 02:37:40
【问题描述】:
我正在尝试验证文件是否存在,但问题是文件名中包含方括号,即 c:\test[R] 10005404, Failed with Comments, [S] SiteName.txt。
我尝试使用字符串 .replace 方法,但没有成功。
$a = c:\test\[R] 10005404, Failed with Comments, [S] SiteName.txt
$Result = (Test-Path $a)
# Returns $False even though the file exists.
试过
$a = $a.Replace("[", "`[")
$a = $a.Replace("]", "`]")
$Result = (Test-Path $a)
# Also returns $False even though the file exists.
我们将不胜感激。 谢谢,克里斯M
【问题讨论】:
-
路径名周围应该有引号:
$a = 'c:\test\[R] 10005404, Failed with Comments, [S] SiteName.txt'。这只是一个错字,还是在您的代码中? -
旧 Windows PowerShell Tip of the Week 解释了原因和解决方法。这是来自powershell get-childitem cannot handle filename starting with [ character even with escape character 的重复问题
标签: powershell powershell-2.0 powershell-ise