如何使用PowerShell复制与某些扩展名匹配的目录中的所有文件?

时间:2017-10-15 23:16:39

标签: powershell powershell-v3.0

我之前用批处理文件做过类似的事情

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)并在一个命令中完成所有操作。

1 个答案:

答案 0 :(得分: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' }