2013-09-25 16 views
1

我必須複製一個文件名的文件如下複製項目與文件名奇怪的字符(即[])

$filename = "Sport.EL#15.csv.[]" 

到另一個文件夾。

如果我使用

copy-item -force Sport.EL#15.csv.[] $dest_path 

這是行不通的。

,因爲我這樣做:

foreach ($f in Get-ChildItem $pathfile | Where {!$_.PsIsContainer} | Where {$_.Name -match $filename}){....} 

,誤差

Bad argument to operator '-match': parsing "Sport.EL#15.csv.[]" - Unterminated [] set.. 

我認爲這個問題是[]中的文件名。我該如何解決?

回答

3

嘗試使用:評論後

copy-item -literalpath $filename $dest_path 

編輯:

來解決-match問題的嘗試:

Where {$_.Name -match [regex]::escape($filename)} 

-match採取regular expression值,你需要escape每個特殊字符使用在regex language,以避免這樣的問題。

+0

太棒了,它的工作原理。但我可以解決我的foreach問題.... - 匹配? – rschirin

+0

@rschirin編輯我的答案添加一個技巧,讓我知道... –

+0

太棒了!它完美的工作!!!!謝謝 – rschirin