1

I have almost no powershell experience but was using it as a means to an end to replace possibly problematic characters in FAT32 filenames, such as: , . / ' : etc.

I discovered an article positing use of rename-item -NewName command, whichs works fine inside a specific directory containing files meeting said criteria, but when used at a level above this, I can't figure out how to make the script fully recursive.

I want to replace spaces, apostrophes, instances of periods outside of file extensions, and dollar signs in filenames of audio tracks inside a music library folder that's laid out like so**, running powershell script from music folder to hit everything inside and below that:

X:\home\audio\**music**\[artist_here]\[album_here]\FILE.mp3

Can someone explain the correct syntax to accomplish this?

I also tried using path and /s:

dir X\pathnamehere\ /s | rename-item -NewName {$_.name -replace " ","_"}

but receive another error:

dir : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:1 char:1
+ dir X:\home\Audio\Music\ /s | rename-item -NewName {$_.name -repla ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: (X:\home\Audio\Music:String) 
[Get-ChildItem], ArgumentException
+ FullyQualifiedErrorId : 
DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

dir | rename-item -NewName {$_.name -replace " ","_"}

when running

dir | rename-item -NewName {$_.name -replace " ","_"}

at multiple levels above where files to be renamed are located, I receive the following error for EVERY directory inside \music:

"rename-item : Source and destination path must be different." 

+ dir | rename-item -NewName {$_.name -replace " ","_"}
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : WriteError: 
(X:\home\Audio\Music\artistname:String) [Rename-Item], IOException
+ FullyQualifiedErrorId : 
RenameItemIOError,Microsoft.PowerShell.Commands.RenameItemCommand

Thank you!

P.S. if this can be accomplished with a simpler command prompt batch file, feel free to enlighten me.

Emma L
  • 13
  • 1
  • 3

1 Answers1

2

Found a solution here: Recursively renaming files with Powershell

Here's their example:

Get-ChildItem -File -Recurse | % { Rename-Item -Path $_.PSPath -NewName $_.Name.replace(".mkv.mp4",".mp4")}

In powershell Dir is an alias of Get-ChildItem and % is 'ForEach'. You were pretty close.

JonR85
  • 700
  • 4
  • 12
  • Thank you! This is working. How could I chain multiple replacement criteria? Should I separate multiple Get-ChildItem -File - Recurse | ...." commands with ; each with a different $_.Name.replace("X","y")? or is there a way to string multiple replacement options in the same command? Would renaming folders as well as files require just an additional switch ala -file, or would that perhaps get complicated? – Emma L Dec 27 '18 at 00:12
  • I think you can just specify addition replace statements by doing -NewName $_.Name.replace(".mkv.mp4",".mp4").replace("foo","bar").replace("even","more")} Rename-item works on folders the same as it does files. But your get-childitem will never feed folders to the pipeline. However you might want to run that as a separate command anyways as you might run into things being renamed that you weren't expecting to be renamed, – JonR85 Dec 27 '18 at 03:04
  • As `Rename-Item` accepts piped input, the `ForEach-Object` isn't neccessary => `Get-ChildItem -File -Recurse | Rename-Item -NewName {$_.Name.replace(".mkv.mp4",".mp4")}` –  Dec 27 '18 at 11:26