如何使用PowerShell将所有文件复制到与特定扩展名匹配的目录中?
问题描述:
我做了这样的事情之前,批处理文件如何使用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)并在一个命令中完成所有操作。
答
使用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
*有一定是这样的一种优雅的方式* - 乞丐不能挑肥拣瘦。 'gci * .exe,*。txt | %{copy -L $ _ -D($ _。name +'。deploy')}' – TessellatingHeckler
@WhiskerBiscuit:请求帮助之前,您尝试了哪些Powershell代码? – Manu