Author Posts

December 5, 2014 at 8:47 am

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

December 5, 2014 at 11:36 am

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.

December 7, 2014 at 11:51 am

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