Command line bulk rename using FOR

What with all the cleverness going on with PowerShell I still find it easier sometimes to just use the built in command prompt for some things. One such example was a recent download of 100 MP3 files from bleep.com, their top 100 tracks of 2011. Each file was named thus:
Bleep_100_Tracks_2011-<artist name>-<track title>.mp3

This is a nuisance as my car audio system displays the file name for mp3 files, and it can only fit about 15 characters on screen at once. It displays the first 15 characters, then after a short delay the next fifteen, and so on. So having the “Bleep_100_Tracks_2011-” at the start of each filename is really annoying as a reasonable percentage of the time the screen doesn’t display either the track name or artist.

So, time for a quick rename, and a good example of for FOR command in action. Change into the folder containing the files first.

for /f "delims=- tokens=1,2,3" %i in ('dir bleep*.* /b') do @ren "%i-%j-%k" "%j - %k"
  • Using the /f option allows you to work on a file set or, as I prefer, the output of a command. To use a command output, put the command in single quotes within the brackets. i.e. ‘dir bleep*.* /b’ in the example above.
  • Dir /b gives a bare directory listing, no headers or footers or file size info, just the filenames.
  • The bit in double quotes sets the delimiter character to a hyphen (the default is space), and says that once the input text string has been split into chunks (based on the delimiter(s)) which chunk numbers we want to capture.
  • %i is the name of the variable to store the first chunk into, subsequent chunks will be stored in variables with the next letters of the alphabet, thus chunk 2 goes into %j and chunk 3 goes into %k.
  • The words “in” and “do” are compulsory, they’re part of the syntax of the FOR command.
  • The last bit does the rename, I use @ immediately before the REN command so that the REN command line isn’t echoed to screen before it’s executed, makes it a bit faster and depending on the command can make the screen output a bit neater (though not in this case).

Here’s what the various bits would do in a folder only containing one file (for ease).

dir bleep*.* /b
Bleep_100_Tracks_2011-Paul Woolford-Stolen (Original Mix).mp3
ren "Bleep_100_Tracks_2011:Paul Woolford:Stolen (Original Mix).mp3" "Paul Woolford - Stolen (Original Mix).mp3"

The resultant filename was therefore: Paul Woolford – Stolen (Original Mix).mp3

Tip: I used @echo %i:%j:%k initially to check that the delimiter etc. was correctly breaking up the command input.

Clearly there are lots of other options for FOR, unsurprisingly you can see them by using for /?. I know some people who don’t “understand” FOR, but once you get your head around how it works it can be really handy.

This entry was posted in Scripting, Windows and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s