6

Morning Powershell users, I'm trying to copy files listed in a .CSV document from one directory to another based on their file name. My .CSV document is just one column of filenames, it looks like this:

2.jpg
4.jpg
5.jpg
8.jpg
12.jpg

I have a folder of .jpgs from 1-900 in G:\Numbered\ and a destination folder for where the files should go at G:\Selections. This seems like it should be relatively simple but I think I'm missing something in my code. This is what I have so far:

import-CSV G:\fileList1.csv | foreach {copy-item G:\Numbered G:\Selections}

I looked around and I think I may need to use Get-Childitem but I'm not sure where. Any help is greatly appreciated, thank you!

0

1 Answer 1

10

Assuming your CSV look like this:

YourColumnName
2.jpg
4.jpg
n.jpg

Try this:

Import-Csv fileList1.csv | ForEach {Copy-Item "G:\Numbered\$($_.YourColumnName)" G:\Selections }

But, if your CSV has only 1 column without a header, like this:

2.jpg
4.jpg
n.jpg

you should use Get-Content because it's not really a csv file. And your code should be:

Get-Content files.csv  | ForEach {Copy-Item G:\Numbered\$_ G:\Selections }
1
  • Using Get-Content worked perfectly! Thank you for the quick answer. Feb 16, 2016 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.