I bet you’ve run into situations where you have a ton of files that should be partially renamed, but Windows can’t help you with the bulk renaming because it can only do “Your new name (1).jpg” to “Your new name (50).jpg”.
Well, it’s time to whip up a PowerShell session and use some find-replace magic on those files, I say! Here’s the gist of it, in a simple one-liner. Let’s say you want to rename a bunch of mp3 files.
ls *.mp3 | % { ren $_.Name ($_.Name -replace "replace this", "with that") }
- The
%
will do a for-each loop over all mp3 files from thels
command. - The
ren
command will rename the file with the name$_.Name
(the original file name) with whatever comes after. - Between brackets, we will massage the filename into whatever we want. In this example, we are doing a find-replace in the filename, and replacing “replace this” with “with that”.
Here’s a more realistic example. Let’s remove label and release info from mp3 file names, because this makes the file names too long.
ls *.mp3 | % { ren $_.Name ($_.Name -replace " \(Bonk Wave Masters 001 BKM01\)", "") }
This turns a file name like Bonk Master - Feel The Bonk (Bonk Wave Masters 001 BKM01).mp3
into Bonk Master - Feel The Bonk.mp3
.
Mind the extra slash in front of the brackets. This makes sure that PowerShell takes that literally, as it interprets the brackets as part of a regular expression otherwise.
Whatever is inside the brackets is what the new file name will be, so you can add to it, replace and remove text.
The post bulk renaming files with powershell appeared first on n3wjack's blog.