简体   繁体   English

Powershell基于CSV内容复制文件

[英]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. Morning Powershell用户,我正在尝试根据文件名将.CSV文档中列出的文件从一个目录复制到另一个目录。 My .CSV document is just one column of filenames, it looks like this: 我的.CSV文档只是一列文件名,它看起来像这样:

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. 我在G:\\ Numbered \\中有一个来自1-900的.jpgs文件夹,以及文件应该放在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. 我环顾四周,我想我可能需要使用Get-Childitem,但我不知道在哪里。 Any help is greatly appreciated, thank you! 非常感谢任何帮助,谢谢!

Assuming your csv look like this : 假设你的csv看起来像这样:

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 : 但是如果你的csv只有一列没有列,就像这样:

2.jpg
4.jpg
n.jpg

You should use Get-Content because it's not really a csv file. 您应该使用Get-Content因为它不是真正的csv文件。 And your code should be : 你的代码应该是:

Get-Content files.csv  | ForEach {Copy-Item G:\Numbered\$_ G:\Selections }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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