如何使用 Powershell 使用文本文件作为新名称的输入来重命名文件夹中的文件?

发布于 2025-01-09 20:23:27 字数 531 浏览 1 评论 0原文

大家好,很抱歉问这个蹩脚的问题,但我真的在这里很挣扎。 我可以将要重命名的文件以及文本文件的内容放入变量中,如下所示:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦醒时光 2025-01-16 20:23:27

使用变量来跟踪您已使用的文件名数量:

$filestochange = Get-ChildItem -Path "c:\test"
$FileNames = Get-Content "c:\src.txt"

# use this variable to keep track of the next file name to use
$counter = 0

foreach($file in $filestochange){
  # Remember to check if we have any file names left to use
  if($counter -lt $FileNames.Length){
    # Rename the next file to the next name in `$FileNames`, then increment counter
    $file |Rename-Item -NewName $FileNames[$counter++]
  } 
  else {
    Write-Warning "We ran out of file names!"
    break
  }
}

Use a variable to keep track of how many file names you've already used:

$filestochange = Get-ChildItem -Path "c:\test"
$FileNames = Get-Content "c:\src.txt"

# use this variable to keep track of the next file name to use
$counter = 0

foreach($file in $filestochange){
  # Remember to check if we have any file names left to use
  if($counter -lt $FileNames.Length){
    # Rename the next file to the next name in `$FileNames`, then increment counter
    $file |Rename-Item -NewName $FileNames[$counter++]
  } 
  else {
    Write-Warning "We ran out of file names!"
    break
  }
}
遇见了你 2025-01-16 20:23:27

马蒂亚斯的代码对我有用,我是一个真正的新手。谢谢你!

我试图用希腊语单词/文本重命名 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

文件与新名称正确关联。

$filestochange = Get-ChildItem -Path "C:\Users\FOLDER_path_with_files_to_have_names_changed"

$FileNames = Get-Content "C:\Users\FILE_path_to_text_file_with_new_names_having_the_approriate_file_extension"

# use this variable to keep track of the next file name to use
$counter = 0

foreach($file in $filestochange){
  # Remember to check if we have any file names left to use
  if($counter -lt $FileNames.Length){
    # Rename the next file to the next name in `$FileNames`, then increment counter
    $file |Rename-Item -NewName $FileNames[$counter++]
  } 
  else {
    Write-Warning "We ran out of file names!"
    break
  }
}

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.

$filestochange = Get-ChildItem -Path "C:\Users\FOLDER_path_with_files_to_have_names_changed"

$FileNames = Get-Content "C:\Users\FILE_path_to_text_file_with_new_names_having_the_approriate_file_extension"

# use this variable to keep track of the next file name to use
$counter = 0

foreach($file in $filestochange){
  # Remember to check if we have any file names left to use
  if($counter -lt $FileNames.Length){
    # Rename the next file to the next name in `$FileNames`, then increment counter
    $file |Rename-Item -NewName $FileNames[$counter++]
  } 
  else {
    Write-Warning "We ran out of file names!"
    break
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文