0

我正在尝试编写一个 PowerShell 脚本,该脚本将从源文件夹中复制一部分文件并将它们放入目标文件夹中。我玩了半天的“copy-item”和“remove-item”,无法得到想要的或一致的结果。

例如,当我多次运行以下 cmdlet 时,文件最终位于不同的位置?!?!:

copy-item -Path $sourcePath -Destination $destinationPath -Include *.dll -Container -Force -Recurse

我一直在尝试我能想到但找不到正确解决方案的所有选项和命令组合。由于我确信我没有做任何非典型的事情,我希望有人可以减轻我的痛苦并为我提供正确的语法来使用。

源文件夹将包含大量具有各种扩展名的文件。例如,以下所有情况都是可能的:

  • .dll
  • .dll.config
  • 。可执行程序
  • .exe.config
  • .lastcode分析成功
  • .pdb
  • .Test.dll
  • .vshost.exe
  • .xml
  • 等等

该脚本只需要复制 .exe、.dll 和 .exe.config 文件,不包括任何 .test.dll 和 .vshost.exe 文件。如果目标文件夹不存在,我还需要脚本来创建它们。

感谢任何帮助我前进。

4

2 回答 2

1

尝试:

$source = "C:\a\*"
$dest =  "C:\b"

dir $source -include *.exe,*.dll,*.exe.config -exclude *.test.dll,*.vshost.exe  -Recurse | 
% {

 $sp = $_.fullName.replace($sourcePath.replace('\*',''), $destPath)

 if (!(Test-Path -path (split-path $sp)))
    {
     New-Item (split-path $sp) -Type Directory
    } 

    copy-item $_.fullname  $sp -force
  }
于 2012-11-17T22:27:09.260 回答
0

只要文件在一个目录中,以下应该可以正常工作。它可能比需要的更冗长,但它应该是一个很好的起点。

$sourcePath = "c:\sourcePath"
$destPath = "c:\destPath"

$items = Get-ChildItem $sourcePath | Where-Object {($_.FullName -like "*.exe") -or ($_.FullName -like "*.exe.config") -or ($_.FullName -like "*.dll")}

$items | % {
    Copy-Item $_.Fullname ($_.FullName.Replace($sourcePath,$destPath))
}
于 2012-11-15T19:23:14.490 回答