简体   繁体   中英

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!

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 have only 1 column without column 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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM