如何将源路径列表中定义的文件复制到cmd或Powershell中的输出路径列表中?

问题描述

我在两个变量中都有一个源文件列表和一个目标文件列表,所有变量都用空格分隔:

$srcFiles = C:/srcpath1/srcfile1 C:/srcpath2/srcfile2 ...
$dstFiles = C:/dstpath1/srcfile1 C:dstpath2/srcfile2 ...

如何使用cmd或powershell将所有src文件复制到所有输出位置?

解决方法

假设您有两个适当排序的数组,则可以执行以下操作:

param(
    $srcFiles = @("$PSScriptRoot\src\file1.txt","$PSScriptRoot\src\file2.txt"),$destFiles = @("$PSScriptRoot\dest\file1.txt","$PSScriptRoot\dest\file2.txt")
)

for ($i = 0; $i -lt $srcFiles.Length; $i++) {
    Copy-Item -Path $srcFiles[$i] -Destination $destFiles[$i] 
}

我个人可能会为此使用哈希表,而不是两个数组。

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...