File Copy to Multiple locations Using a CSV File

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of H Man H Man 2 years ago.

  • Author
    Posts
  • #20944
    Profile photo of H Man
    H Man
    Participant

    I am currently scanning a directory and matching file names and then copying them to various file share locations (each file will have multiple desitnations). I am hard coding the locations and would like to instead store these locations in a csv file. The csv file should have 2 fields. , the destination column = path for the copy; and the string column = to identify the file file to be copied

    heres what I am doing now

    #Find and Move
    $entries = Get-ChildItem $filepath 
    foreach ($entry in $entries) {
    
    $Gen2 =    'C:\Lab\Gen2'
    $Billing = 'C:\Lab\Billing'
    $PdgMtnService  = 'C:\Lab\PdgMtnService'
    
    
    if ($entry.FullName -like '*MOTION TO COMPEL*'){  copy-item -Path $entry.FullName -Destination $Billing  -Force     }
    if ($entry.FullName -like '*MOTION TO COMPEL*'){  copy-item -Path $entry.FullName -Destination $Gen2 -Force    }
    if ($entry.FullName -like '*MOTION TO COMPEL*'){  copy-item -Path $entry.FullName -Destination $PdgMtnService -Force } 
    }
    
    

    thank you in advance

  • #20945
    Profile photo of Craig Duff
    Craig Duff
    Participant

    Maybe something like this:

    $csv = Import-Csv -Path "T:\46\files.csv"
    $filepath = "T:\46\src"
    $entries = Get-ChildItem $filepath
    ForEach ($entry in $entries) {
        $destinations = $csv | 
        Where-Object {$_.file -eq $entry.FullName} | 
        Select-Object -ExpandProperty destination
        ForEach ($destination in $destinations) {
            Copy-Item $entry.FullName $destination
        }
    }

    And a csv like:

    "file","destination"
    "t:\46\src\New Text Document.txt","t:\46\dest\a\"
    "t:\46\src\New Text Document.txt","t:\46\dest\b\"
    "t:\46\src\New Text Document (2).txt","t:\46\dest\c\"
    "t:\46\src\New Text Document (2).txt","t:\46\dest\d\"

  • #20946
    Profile photo of H Man
    H Man
    Participant

    Thank you for the response!

    The copy part is perfect! But i need the csv 2 have only 2 columns "Strng2Fing" and "destination"

    I need to incorporate the "Strng2Fing"

     if ($entry.FullName -like '*MOTION TO COMPEL*'){  copy-item -Path $entry.FullName -Destination $Billing  -Force     } 

    And a csv like:

    "Strng2Fing" ,"destination"

    "MOTION TO COMPEL" , "t:\46\dest\a\"
    "MOTION TO COMPEL" , "t:\46\dest\b\"
    "MOTION TO COMPEL" , "t:\46\dest\ac"

    find 1 file copy to 3 different locations

  • #20951
    Profile photo of H Man
    H Man
    Participant

    I Solved it

    $csv = Import-Csv -Path "C:\Users\jcool\Documents\Test.csv"
    $filepath = 'c:\test'
    Get-ChildItem $filepath | foreach {
     $criteria = $csv
     $find = $csv | select -ExpandProperty find
        #Write-Host $_.fullname
        $a = $_.fullname
    ForEach ($f in $find) {
        If ($a -like "*$f*") {
    
    ForEach ($c in $criteria) {
    
    Copy-Item $_.fullname $c.Destination
    
    }
    }
        }
          }
    
    
    
    

    And a csv like

    "Find", "Destination"
    "Don Jones" , "c:\test\a"
    "Don Jones" , "c:\test\b"

You must be logged in to reply to this topic.