如何使用 Powershell 使用文本文件作为新名称的输入来重命名文件夹中的文件?
大家好,很抱歉问这个蹩脚的问题,但我真的在这里很挣扎。 我可以将要重命名的文件以及文本文件的内容放入变量中,如下所示:
$filestochange = Get-ChildItem -Path "c:\test"
$FileNames = Get-Content "c:\src.txt"
但是如何将其传递给 Rename-Item 命令,以便使用 txt 文件中的每一行来重命名一行中的另一个文件?
我尝试过: Rename-Item -Path $filestochange -NewName $FileNames
出现错误:
Rename-Item:无法将“System.Object[]”转换为参数“所需的类型“System.String”小路'。不支持指定的方法。
我也尝试过 ForEach,但我不知道如何在这种情况下为 rename-item 命令添加另一个变量。
我相信这很简单,一旦有人知道如何使用所有这些 $_。 {}""
请帮助我走得更远。非常感谢!
Hi everybody and sorry for this lame question , but I´m truly struggling here.
I can put files to be renamed into variable as well as content of the text file, like this:
$filestochange = Get-ChildItem -Path "c:\test"
$FileNames = Get-Content "c:\src.txt"
But how to pass it to Rename-Item command so every line in txt file was used to rename another file in a row?
I tried : Rename-Item -Path $filestochange -NewName $FileNames
Error comes up:
Rename-Item : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'Path'. Specified method is not supported.
I was trying ForEach also, but I dont know how to put another variables for the rename-item command it that case as well.
I believe it is very simple, once somebody knows how to use all those $_. {}""
Please, help me go further. ¨Many thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用变量来跟踪您已使用的文件名数量:
Use a variable to keep track of how many file names you've already used:
马蒂亚斯的代码对我有用,我是一个真正的新手。谢谢你!
我试图用希腊语单词/文本重命名 mp3 文件...全部 9000 个...
稍稍说一下,就我而言,我必须将新文件名列表保存为 unicode 文本(对于希腊语单词部分)并且还将“.mp3”添加到新文件名列表中的文件名中。
注意:我只修改了路径的前两行代码,代码中的其他内容没有被修改。
C:\Users\FOLDER_path_with_files_to_have_names_changed
C:\Users\FILE_path_to_text_file_with_new_names_having_the_approriate_file_extension
所以我从: 01-S.mp3------->κύριος.mp3
文件与新名称正确关联。
Mathias's code worked for me and I am a true novice. Thank you!
I was trying to rename mp3 files with greek words/text...all 9000 of them...
A slight aside, in my case I had to save my list of new file names as unicode text (for the greek word part) and also add ".mp3" to the filename in the list of new filenames.
NOTE: I only modified the first two lines of code for the paths, nothing else in the code was modified.
C:\Users\FOLDER_path_with_files_to_have_names_changed
C:\Users\FILE_path_to_text_file_with_new_names_having_the_approriate_file_extension
So I went from: 01-S.mp3------->κύριος.mp3
The files were correctly associated with the new names.