我試圖驗證文件的存在,但問題在於文件名中的名稱中有括號,例如c:\ test [R] 10005404,Failed與評論,[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.
思想將不勝感激。 謝謝,聖油
應該在路徑名稱周圍引用:'$ a ='c:\ test \ [R] 10005404,失敗並返回評論,[S] SiteName.txt''。這只是一個錯字,還是在你的代碼? – Rynant 2012-04-13 18:03:59
舊的Windows [PowerShell本週提示](http://technet.microsoft.com/en-us/library/ff730956.aspx)解釋了原因和解決方法。這是一種重複的問題[powershell get-childitem無法處理文件名以[字符即使使用轉義字符]開頭(http://stackoverflow.com/a/9508802/608772) – JPBlanc 2012-04-14 04:45:40