powershell - Powershell - Read CSV contents, copy file from a network drive to local machine
问题描述
I am currently in the process of restructuring a Windows server file directory and need to bulk rename over 10,000 files all in different folders but most have duplicate filenames (e.g 0001.pdf, 0002.pdf)
I have written a Powershell script to help me do this but I am having some trouble with the execution.
The script reads a csv file (export of all the files in the folder structure - export.csv) then creates the variables $OldFilename
& $NewFileName
. The variables are then used to locate the old file location and copy it to a new destination using the Copy-Item
command for each row in the CSV.
Code
$csv = import-csv c:\tmp\test.csv
$csv | foreach-object {
$OldFileName =$_.OldFileName
$NewFileName = $_.NewFileName
}
$csv | ForEach-Object {
$OldFileName =$_.OldFileName
$NewFileName = $_.NewFileName
Copy-Item $OldFileName -Destination $NewFileName
}
CSV is formatted as such
OldFileName,NewFileName
"X:\Folder1\0001.pdf","C:\temp\File0001.pdf"
"X:\Folder2\0001.pdf","C:\temp\File0002.pdf"
"X:\Folder3\0001.pdf","C:\temp\File0003.pdf"
Code does not error when run but files do not appear.
A Write-Output
displays the correct file path but a Write-Host((Get-Item $file).length/1MB)
does not retrieve the files size which leads me to believe there is an issue with the variable!?
Something I need to add?
Many Thanks
Jonny
解决方案
推荐阅读
- python - 如何在 Pandas Python 上设置多列?
- java - 如何检查用户是否存在于 Radius 服务器中
- laravel - Laravel 验证:在不需要字段时排除大小检查
- javascript - Javascript 或 Ramda 按属性转换 JSON
- python - 如何从 Spark 结构化流写入 RDMS?foreach 行里面是什么?
- java - 将 apache cxf 升级到 3.4.1 导致错误
- typescript - 打字稿类此参考未按预期工作
- azure - Azure Function 崩溃的日志和内存转储在哪里?
- typescript - 带有“v-for”的 b-tab 中的条件属性“lazy”
- amazon-web-services - CloudWatch - 传输后删除日志