Use Csv to determine Which File Copy Path

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

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

    I have a folder with a 1000 files.

    I want to use a csv file for 2 things

    Field # 1 "Find" the string in the file name
    Field # 2 "Destination" is the path the file should be copied too

    There are multiple paths and my foreach loop is coping them to all the paths but not the path aligned with the find string Please Help

    heres what I have so for

    $csv = Import-Csv -Path "C:\Users\ldiskin\Desktop\Matrix2.CSV"
    $filepath = 'C:\SampleGroup'
    
    
    
    $entries = Get-ChildItem $filepath
    ForEach ($entry in $entries) {
    $destinations = $csv | Select-Object -ExpandProperty destination
    $find = $csv | select -ExpandProperty find
    $a = $entry.fullname
    
    
    ForEach ($f in $find) {
        If ($a -like "*$f*") {
    
     ForEach ($destination in $destinations) {
            Copy-Item $a $destination
        }
         }# end If 
            } # foreach # 2
                } # foreach main
    

    The Csv file is attached

  • #20967
    Profile photo of Matt McNabb
    Matt McNabb
    Participant

    Does something like this work?

    $Csv = Import-Csv -Path "C:\Users\ldiskin\Desktop\Matrix2.CSV"
    $FilePath = 'C:\SampleGroup'
    $Entries = Get-ChildItem $filepath
    
    ForEach ($Entry in $Entries)
    {
        $FileName = $Entry.FullName
        ForEach ($String in $Csv.Find)
        {
            If ($Entry.FullName -match $String)
            {
                $Match = $Csv | Where Find -eq $String
                Copy-Item $Entry.FullName $Match.Destination
            }
        }
    }

    I haven't tested any of this. Also, double-check your filenames to make sure there is not chance that one could match more than one string in the csv find column.

  • #20979
    Profile photo of H Man
    H Man
    Participant

    thank you for the response I am getting this error

    Copy-Item : Cannot convert 'System.Object[]' to the type 'System.String' required
    by parameter 'Destination'. Specified method is not supported.
    At line:13 char:39
    + Copy-Item $Entry.FullName $Match.Destination

You must be logged in to reply to this topic.