0

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!

Kubal
  • 1
  • 1

2 Answers2

3

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
  }
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • Thank you @Mathias. But this: `foreach($file in $filestochange){ $file |Rename-Item $FileNames } ` is not working:( Conditions are not necessary, thanks for them though – Kubal Feb 24 '22 at 14:36
  • @Kubal That's _not_ the code from my answer. You're using `$FileNames` instead of `$FileNames[$counter++]`. – Mathias R. Jessen Feb 24 '22 at 14:38
  • Sadly even whole code of yours is not working on my side, resulting with InputObjectNotBound error – Kubal Feb 24 '22 at 23:02
0

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
  }
}
Seuss
  • 1