[英]PowerShell to copy files to destination's subfolders while excluding certain folders in the destination
So I have danced with this off and on throughout the day and the timeless phrase "There's more than one way to skin a cat" keeps coming to mind so I decided to take to the community. 因此,我整天都在不停地跳着舞,而永不过时的一句“给猫剥皮的方法不止一种”,让我决定加入社区。
Scenario: 场景:
Source folder "C:\\Updates" has 100 files of various extensions. 源文件夹“ C:\\ Updates”具有100个各种扩展名的文件。 All need to be copied to the sub-folders only of "C:\\Prod\\" overwriting any duplicates that it may find.
仅将所有内容复制到“ C:\\ Prod \\”的子文件夹中 ,从而覆盖所有可能找到的重复项。
The Caveats: 注意事项:
The sub-folder names (destinations) in "C:\\Prod" are quite dynamic and change frequently. “ C:\\ Prod”中的子文件夹名称(目的地)非常动态并且经常更改。
A naming convention is used to determine which sub-folders in the destination need to be excluded when the source files are being copied (to retain the original versions). 使用命名约定来确定在复制源文件(保留原始版本)时需要排除目标中的哪些子文件夹。 For ease of explanation lets say any folder names starting with "!stop" should be excluded from the copy process.
为了便于说明,可以说任何以“!stop”开头的文件夹名称都应排除在复制过程之外。 (!stop* if wildcards considered)
(!停止*,如果考虑使用通配符)
So, here I am wanting the input of those greater than I to tackle this in PS if I'm lucky. 所以,在这里,如果我很幸运,我希望得到比我更多的人来解决PS中的问题。 I've tinkered with Copy-Item and xcopy today so I'm excited to hear other's input.
我今天对Copy-Item和xcopy进行了修改,因此很高兴听到其他人的输入。
Thanks! 谢谢!
-Chris -克里斯
Give this a shot: 试一下:
Get-ChildItem -Path C:\Prod -Exclude !stop* -Directory `
| ForEach-Object { Copy-Item -Path C:\Updates\* -Destination $_ -Force }
This grabs each folder (the -Directory
switch ensures we only grab folders) in C:\\Prod that does not match the filter and pipes it to the ForEach-Object
command where we are running the Copy-Item
command to copy the files to the directory. 这将抓取与过滤器不匹配的C:\\ Prod中的每个文件夹(
-Directory
开关确保仅抓取文件夹),并将其通过管道传递到ForEach-Object
命令,在此我们运行Copy-Item
命令将文件复制到目录。
The -Directory
switch is not available in every version of PowerShell; -Directory
开关并非在所有版本的PowerShell中都可用; I do not know which version it was introduced in off the top of my head. 我不知道它是哪个版本推出的。 If you have an older version of PowerShell that does not support
-Directory
then you can use this script: 如果您使用的旧版PowerShell不支持
-Directory
则可以使用以下脚本:
Get-ChildItem -Path C:\Prod -Exclude !stop* `
| Where-Object { $_.PSIsContainer } `
| ForEach-Object { Copy-Item -Path C:\Updates\* -Destination $_ -Force }
To select only sub folders which do not begin with "!stop" do this 要仅选择不以“!stop”开头的子文件夹,请执行此操作
$Source = "C:\Updates\*"
$Dest = "C:\Prod"
$Stop = "^!stop"
$Destinations = GCI -Path $Dest |?{$_.PSIsContainer -and $_.Name -notmatch $Stop }
ForEach ($Destination in $Destinations) {
Copy-Item -Path $Source -Destination $Destination.FullName -Force
}
Edited Now copies all files from Update to subs of Source not beginning with "!stop" The -whatif switch shows what would happen, to arm the script remove the -whatif. 立即编辑将所有文件从Update复制到Source的子目录中,而不以“!stop”开头。-whatif开关显示将发生的情况,以备脚本删除-whatif。
Edit2 Streamlined the script. Edit2简化了脚本。 If also Sub/sub-folders of C:\\Prod shall receive copies include a -rec option to the gci just in front of he pipe.
如果C:\\ Prod的子/子文件夹也将收到副本,则在管道前面的gci包含-rec选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.