将8个文件从文件夹复制到40个其他子文件夹

时间:2013-07-10 13:51:31

标签: powershell copy powershell-v2.0

我试图制作一个脚本,将所有(8个文件)文件从一个文件夹复制到约40个文件夹的特定子文件夹中......

我从这个开头用*来复制该文件夹中的所有文件。 但当然它没有像这样:

带有*的foldername随机更改,并且有一个名为EmailTemplates的子文件夹,它应该可以获取我的所有8个文件。

Copy-Item d:\www\example\*   -destination d:\example\2\*\EmailTemplates

有一个简单的解决方案吗? 提前谢谢!

1 个答案:

答案 0 :(得分:1)

这样的东西可以用来选择目标文件夹吗?

$destination = Get-ChildItem "D:\example\2" -Recurse | ? {
    $_.PSIsContainer -and $_.Name -eq "EmailTemplates"
  }

否则你可能必须像这样确定目的地:

$destination = Get-ChildItem "D:\example\2" |
  ? { $_.PSIsContainer } |
  % { Join-Path $_.FullName "EmailTemplates" } |
  ? { Test-Path -LiteralPath $_ } | Get-Item

然后像这样复制文件:

Get-ChildItem "d:\www\example" | ? { -not $_.PSIsContainer } | % {
  Copy-Item $_.FullName $destination.FullName
}
相关问题