统一文件集

时间:2018-05-22 14:54:17

标签: powershell

我在powershell脚本中处理了一堆带有ForEach-Object循环的文件。

Get-ChildItem -Path "<path>" -Recurse | ForEach-Object { 
    # Do something complicated   
}

不幸的是,我有多个文件路径,所以我必须这样写:

Get-ChildItem -Path "<path>" -Recurse | ForEach-Object { 
    # Do something complicated   
}
Get-ChildItem -Path "<path2>" -Recurse | ForEach-Object { 
    # CAUTION, this block exists 2x in this file!
    # Do the same complicated stuff with the other files
}

有什么方法可以&#34;聚合&#34;变量中的文件只在一个循环中处理它们?

1 个答案:

答案 0 :(得分:3)

Get-ChildItem cmdlet的-Path参数支持数组。你可以这样做:

Get-ChildItem -Path <dir1>,<dir2> | ForEach-Object {
  ...
}

或者:

<dir1>,<dir2>[,...] | Get-ChildItem | ForEach-Object {
  ...
}