2013-06-18 22 views
0

我需要對以下Powershell腳本進行更改,但我需要將結果文件寫入不同的路徑。我們稱之爲$destPath我無法使Powershell 2.0將文件從一個路徑移動到另一個路徑

考慮:

Get-ChildItem $sourcePath | % { [system.io.file]::Move($_.fullname, ($_.FullName -replace '\[|\]|-|,|\(|\)', '')) }

基於我move語法的理解,$_.fullname是我原來的文件,$_.FullName -replace...爲新的文件名。 Hoever,當我嘗試使用$destPath.FullName -replace時,出現空白文件名不合法的錯誤。顯然,Powershell並不認爲這是move命令的有效路徑名。

我錯過了什麼?

回答

1

由於您沒有在上下文中提及$ destPath - 可能您沒有定義變量$ destPath?這是不太可能的,只是試圖縮小範圍。

另一種方式來完成這項工作:

重命名子項先在原來的位置。然後移動它們。

get-childitem *.txt | rename-item -newname {$_.name -replace 'o','O'} 

get-childitem *.txt | % {move-item -path $_.fullname -destination .\Test-files} 

但我更喜歡你的一個襯墊:)

+0

是的,我做了定義$ destPath ...這是我檢查的第一件事。 :)但thnaks提醒;你不知道有多少次這是簡單的問題。我可能會把它分成兩行,只是因爲我無法弄清楚爲什麼單行解決方案不適合我... – dwwilson66

+0

親愛的@ dwwilson66如果您認爲它有意義,您可以將它標記爲「答案」? :) – Peter

相關問題