Home > Software design >  Copy from one directory to a specific destination. Powershell
Copy from one directory to a specific destination. Powershell

Time:05-18

Here is what I have so far. I feel like I need some sort of for each statement or something to copy each source line to each destination line imported from csv. Thoughts?

$source = Import-Csv -header 'Source' C:\Test\source.csv

$destination = Import-Csv -header 'Dest' C:\Test\dest.csv

Copy-Item -Path $source -Destination $destination

CodePudding user response:

To answer the question as asked: File: Source.csv

SourceFile
G:\Test\Excel\JPLTest-1.xlsx
G:\Test\Excel\JPLTest-A.xlsx

File: Dest.csv

DestFile
G:\Test\Excel\JPLTest-2.xlsx
G:\Test\Excel\JPLTest-B.xlsx

Code:

$Source = Import-CSV -Path G:\BEKDocs\Scripts\Source.csv
$Dest   = Import-CSV -Path G:\BEKDocs\Scripts\Dest.csv

#Note: If you use Santiago's suggestion you don't need this test!

If (($Source.Count) -ne ($Dest.Count)) {
  
  "CSV File line counts do not match!"

} #End If

Else {

  For ($Cntr = 0 ; $Cntr -lt $($Source.count) ; $Cntr  ) {

     Copy-Item -Path $($Source.SourceFile[$($Cntr)]) -Destination $($Dest.DestFile[$($Cntr)])

  }  #End For...

} #End Else
  • Related