繁体   English   中英

使用 powershell 从文件夹中复制特定文件

[英]Copy specific files from a folder with powershell

我需要复制许多具有特定名称的特定 pdf 文件。 所有文件都在同一个文件夹中,我在csv中有所有文件名。 我做了一个懒惰的“脚本”,就像这样(显然不是这篇文章中的所有文件):

Copy-Item -Path 391248* -Destination <destination>
Copy-Item -Path 241044* -Destination <destination>
Copy-Item -Path 532350* -Destination <destination>

有没有人有更干净的解决方案?

$fileList  = 'C:\filelist.csv'

Get-Content -Path $fileList | ForEach-Object {

    Copy-Item -Path $_ -Destination 'destination'
}

另一种单衬解决方案是:

cat "The path of csv file.csv" | % {copy -path $_ -destination "The destination"}

我的 CSV 看起来像这样:

Filename
1
2
3
4
5
6
7
8
9

此 csv 不包含文件扩展名,以下代码添加了文件扩展名“.pdf”。 您可以修改它以包含您需要的任何内容。

$csv_location = 'path to your csv'

# 'FileName' is the name of the column inside the csv
$file_names   = ( Import-Csv $csv_location ).FileName
$destination  = 'path of files to be copied into'
$origin       = 'path of files to be copied from'

$file_names | ForEach-Object {

#   This line adds the extension '.pdf' to the filename and file path obtained from $file_names and $origin, respectively.
    $full_path = $origin + $_ + '.pdf'

#   Copy from origin to ( destination + file name + file extension )
    Copy-Item -Path ( $full_path ) -Destination $destination -Force
}

如果您的 csv 包含文件扩展名,

$full_path = $origin + $_

暂无
暂无

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

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