如何使用列表文件重命名文件? [英] How to rename files using a list file?
问题描述
我有 100 个文件,我想根据相关列表以特定方式重命名这些文件.我不想重复相同的 cmdlet,如下所示,我想将 cmdlet 指向几个包含旧名称和新名称的列表文件(如下所示),比如 old.txt 和 new.txt.如何使用 PowerShell 执行此操作?
I have 100s of files that I would like to rename in a specific manner according to a correlated list. Rather than repeating the same cmdlets, as below, I would like to point the cmdlet to a couple of list files (below) containing the old and new names, say old.txt and new.txt. How do I do this using PowerShell?
示例:
Rename-Item -Path "E:\MyFolder\old1.pdf" -NewName new1.pdf
Rename-Item -Path "E:\MyFolder\old2.tif" -NewName new2.pdf
Rename-Item -Path "E:\MyFolder\old3.pdf" -NewName new3.pdf
列出文件:
old.txt =
old1.pdf
old2.tif
old3.pdf
new.txt =
new1.pdf
new2.tif
new3.pdf
推荐答案
为了避免 old.txt
和 new.txt
文件中的条目之间的错位,您还可以将文件放入 CSV 文件中:
To avoid misalignment between the entries in old.txt
and new.txt
files, you could also put the files into a CSV file:
old1.pdf,new1.pdf
old2.tif,new2.tif
old3.pdf,new3.pdf
现在您可以使用 Import-Csv
导入您的翻译集":
Now you can use Import-Csv
to import your "translation sets":
$Renames = Import-Csv '.\renames.csv' -Header "OldName","NewName"
foreach($File in $Renames) {
Rename-Item $File.OldName $File.NewName
}
这篇关于如何使用列表文件重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!