Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell copy files based on CSV contents

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!

like image 530
Tim Troiano Avatar asked Feb 16 '16 16:02

Tim Troiano


1 Answers

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 }
like image 69
Adeel ASIF Avatar answered Sep 20 '22 19:09

Adeel ASIF