将文件从多个文件夹移动到相应的目标文件夹

时间:2018-08-28 17:56:12

标签: powershell

我正在尝试找出如何将文件从源位置移动到目标位置的方法。

我已经厌倦了下面的代码,但是无法正常工作:(

$SrcLocations = @('Src1','Src2','Src3')
$DestLocations = @('dest1','dest2','dest3')


function Move-Files {
  Write-Output "$(Get-TimeStamp): $SrcLocations file move to Remote Storage started" | Out-file $OutputLogFile -append
  $FileMoves = get-childitem $SrcLocations
  Foreach ($file in $FileMoves) {
  Move-Item -Path $SrcLocations -Destination $DestLocations -force
  if (!$?) {
  "File $($file.name) failed moving to Remote Storage" | out-file $OutputLogFile -append
           }
  }
 Time-Delay-Move-Files
 Write-Output "$(Get-TimeStamp): File moved to $DestLocations" | Out-file $OutputLogFile -append
}

2 个答案:

答案 0 :(得分:0)

对于每个情况,都不理想,因为您需要源数组和目标数组中都没有值。假设两个数组的长度相同,则可以使用For循环,例如:

$SrcLocations = @('Src1','Src2','Src3')
$DestLocations = @('dest1','dest2','dest3')


For($i = 0; $i -lt $SrcLocations.Count; $i++){
    Write-Host "Moving $($SrcLocations[$i]) to $($DestLocations[$i])"
}

输出

Moving Src1 to dest1
Moving Src2 to dest2
Moving Src3 to dest3

答案 1 :(得分:0)

谢谢:)

添加了移动项,并且运行正常。

$SrcLocations = @('Src1','Src2','Src3')
$DestLocations = @('dest1','dest2','dest3')


For($i = 0; $i -lt $SrcLocations.Count; $i++){
    Move-Item -Path $($SrcLocations[$i]) -Destination $($DestLocations[$i]) -force
    Write-Host "Moving $($SrcLocations[$i]) to $($DestLocations[$i])"
}