我正在尝试使用robocopy
将c:\ users文件夹复制到新目标位置。我的嵌套foreach
语句出现了问题:
$users = @(
'jack'
'Jon'
)
$news = @(
'jacknew'
'JonNew'
)
foreach ($userx in $users) {
foreach ($new in $news) {
$source = "c:\users\$($userx)"
$destination = "d:\users\$($new)"
robocopy $source $destination /MIR /LOG:c:\robocopy.txt /NP
}
}
将两个文件夹复制到d:\ jacknew文件夹。
答案 0 :(得分:2)
您有两个foreach
循环,因此您将执行robocopy
4次:
robocopy c:\users\jack d:\users\jacknew /MIR /LOG:c:\robocopy.txt /NP
robocopy c:\users\jack d:\users\jonnew /MIR /LOG:c:\robocopy.txt /NP
robocopy c:\users\Jon d:\users\jacknew /MIR /LOG:c:\robocopy.txt /NP
robocopy c:\users\Jon d:\users\jonnew /MIR /LOG:c:\robocopy.txt /NP
如果您要将jack
和jon
复制到相应的文件夹jacknew
和jonnew
,您可以使用单个foreach
循环,只需追加{ {1}}复制时; e.g:
new