2017-10-15 121 views
-2

我做了這樣的事情之前,批處理文件如何使用PowerShell將所有文件複製到與特定擴展名匹配的目錄中?

copy "%APPPATH%\*.exe" "%APPPATH%\*.exe.deploy" 

所以我想,如果我有一個目錄下的所有.exe文件複製到`.exe.deploy」

所以:

a.exe 
b.exe 
c.foo 
d.bar 

我想結束了:

a.exe 
b.exe 
c.foo 
d.exe 
a.exe.deploy 
b.exe.deploy 
d.exe.deploy 

有一個這樣做的優雅方式。 BONUS我也想指定多個擴展名(* .exe,* .txt,* .blob)並在一個命令中完成所有操作。

+2

*有一定是這樣的一種優雅的方式* - 乞丐不能挑肥揀瘦。 'gci * .exe,*。txt | %{copy -L $ _ -D($ _。name +'。deploy')}' – TessellatingHeckler

+0

@WhiskerBiscuit:請求幫助之前,您嘗試了哪些Powershell代碼? – Manu

回答

0

使用PowerShell你要複製的文件和管道的結果枚舉到Copy-Item的cmdlet:

Get-ChildItem $env:APPPATH -Filter *.exe | 
    Copy-Item -Destination { $_.FullName + '.deploy' } 

注意-Filter只支持一個字符串。如果你想通過多個擴展你需要使用-Include(但只能結合-Recurse):

Get-ChildItem $env:APPPATH -Include *.exe,*.foo -Recurse | 
    Copy-Item -Destination { $_.FullName + '.deploy' } 
+0

啊,我不知道$ _ pipe的語法。這有幫助 – WhiskerBiscuit

相關問題