使用更好的PowerShell脚本将文件应用于多个文件夹

发布于 2025-01-29 01:06:52 字数 1169 浏览 2 评论 0原文

我正在从事一个项目,必须经常将文件应用于多个文件夹。我正在尝试学习一些PowerShell命令,以使其更轻松一些。我想到了以下脚本,但是我觉得这太冗长了,可以用一个更好的脚本进行蒸馏:

[string]$sourceDirectory  = "C:\Setup\App Folder Files\*"
# Create an array of folders
$destinationDirectories = @(
'C:\Users\GG_RCB1\Documents\',
'C:\Users\GG_RCB2\Documents\',
'C:\Users\LA_RCB1\Documents\',
'C:\Users\PR_RCB1\Documents\',
'C:\Users\PQ_RCB1\Documents\',
'C:\Users\PQ_RCB2\Documents\',
'C:\Users\XC_RCB1\Documents\',
'C:\Users\XC_RCB2\Documents\',
'C:\Users\XC_RCB3\Documents\',
'C:\Users\XC_RCB4\Documents\',
'C:\Users\XC_RCB5\Documents\',
'C:\Users\XC_RCB6\Documents\',
'C:\Users\XC_RCB7\Documents\',
'C:\Users\XC_RCB8\Documents\')
# Perform iteration to create the same file in each folder
foreach ($i in $destinationDirectories) {
    Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $i
}

我进入此过程,知道用户文件夹区域中的每个文件夹都将具有相同的格式:_rcb<#> \ documents \

我知道我可以使用此代码循环浏览这些文件:

Get-ChildItem -Path 'C:\Users'| where-object {$_.Name -match "^[A-Z][A-Z]_RCB"}

我不确定该怎么做是在该循环中如何钻入文档文件夹并执行副本。我想避免必须继续更新第一个代码示例的数组,尤其是当我知道用户文件夹中的子文件夹的命名约定时。我只是在寻找一种更干净的方法来做到这一点。

感谢您的建议!

I'm working on a project where I have to apply a file to multiple folders every so often. I'm trying to learn some PowerShell commands to make this a little easier. I came up with the following script, which works, but I feel that this is too verbose and could be distilled down with a better script:

[string]$sourceDirectory  = "C:\Setup\App Folder Files\*"
# Create an array of folders
$destinationDirectories = @(
'C:\Users\GG_RCB1\Documents\',
'C:\Users\GG_RCB2\Documents\',
'C:\Users\LA_RCB1\Documents\',
'C:\Users\PR_RCB1\Documents\',
'C:\Users\PQ_RCB1\Documents\',
'C:\Users\PQ_RCB2\Documents\',
'C:\Users\XC_RCB1\Documents\',
'C:\Users\XC_RCB2\Documents\',
'C:\Users\XC_RCB3\Documents\',
'C:\Users\XC_RCB4\Documents\',
'C:\Users\XC_RCB5\Documents\',
'C:\Users\XC_RCB6\Documents\',
'C:\Users\XC_RCB7\Documents\',
'C:\Users\XC_RCB8\Documents\')
# Perform iteration to create the same file in each folder
foreach ($i in $destinationDirectories) {
    Copy-item -Force -Recurse -Verbose $sourceDirectory -Destination $i
}

I go into this process knowing that every folder in the User folder area is going to have the same format: _RCB<#>\Documents\

I know that I can loop through those files using this code:

Get-ChildItem -Path 'C:\Users'| where-object {$_.Name -match "^[A-Z][A-Z]_RCB"}

What I'm not sure how to do is to how, within that loop, drill down to the Documents folder and do the copy. I want to avoid having to keep updating the array from the first code sample, particularly when I know the naming convention of the subfolders in the Users folder. I'm just looking for a cleaner way to do this.

Thanks for any suggestions!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2025-02-05 01:06:52

嗯,我会继续发布我的想法。不要从 @mathias 建议中提出建议,但要提供我的解决方案,这是我的看法:

Get-ChildItem -Path "C:\users\[A-Z][A-Z]_RCB*\documents" | 
    Copy-Item -Path $sourceDirectory -Destination { $_.FullName } -Recurse -WhatIf

由于每个人都喜欢可以满足您需求的“ 单线”。 Get-ChildItem在其路径中接受通配符,这使我们一口气完成了这一点。鉴于您的目录是...

  1. 与相同的命名模式一致,
    • [az] [az] _*
  2. ,文件夹目的地相同。
    • 文档

幸运的是,copy-item也具有一些很酷的功能,例如能够使用脚本块,该脚本将允许通过$_。FullName属性是目标,而它们一次被传递给管道。


当您决定结果是您之后的结果时,请删除Whatif公共参数。

Ehh, I'll go ahead and post what I had in mind as well. Not to take away from @Mathias suggestion in the comments, but to offer my solution, here's my take:

Get-ChildItem -Path "C:\users\[A-Z][A-Z]_RCB*\documents" | 
    Copy-Item -Path $sourceDirectory -Destination { $_.FullName } -Recurse -WhatIf

Since everyone loves the "One-Liners" that can accomplish your needs. Get-ChildItem accepts wildcard-expressions in it's path which let's us accomplish this in one go. Given that your directories are...

  1. consistent with the same naming pattern,
    • [A-Z][A-Z]_*
  2. and the folder destination is the same.
    • Documents

Luckily, Copy-Item also has some cool features on it's own such as being able to use a script block that will allow the passing of $_.FullName property as it's destination, while they are passed down the pipeline one at a time.


Remove the -WhatIf common parameter when you've dictated the results are what you're after.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文