Garrett
Garrett

Reputation: 649

Filter Get-Childitem to contents of CSV

I am trying to filter a Get-Childitem cmdlet with the contents of a CSV.

CSV Contents:

Files
------
B000000001
B000000002

Actual File Names:

B000000001.PT01.jpg
B000000002.PT03.jpg

This is what I have:

$ASINs = Import-Csv "C:\share_test.csv"
foreach($ASIN in $ASINs){
   echo $ASIN
   Get-ChildItem -Path "C:\test1" -Filter "*$ASIN*" | Copy-Item -Destination "C:\test2"
}

The alike items are not copying, but echoing the Get-Childitem cmdlet is also not picking up the files. Can anyone point out the error in my code?

Thanks!

Upvotes: 1

Views: 762

Answers (1)

BenH
BenH

Reputation: 10044

When you use Import-CSV the header of each column becomes the property name. So each row of your csv is an object with a Name property rather than an array of strings like you would get from Get-Content.

You can use the subexpression operator $() to interpolate $ASIN.Files inside your filter.

$ASINs = Import-Csv "C:\share_test.csv"
foreach ($ASIN in $ASINs) {
    Get-ChildItem -Path "C:\test1" -Filter "*$($ASIN.Files)*" | Copy-Item -Destination 
    "C:\test2"
}

Upvotes: 3

Related Questions