:: ::

christopher vournazos

chris@chrisvo.co
765.238.9061

Renaming files is boring.

Make it less of a time-sucker by using the command prompt.
2014-05-03
Engineering

If you need to chunk your audio into separate files and name them according to a specific naming convention that your client has provided, you've probably fiddled with renaming files, one at a time.

For a big project with a large number of files, this can be quite a bit of work.

Sure, you can find a nearly infinite selection of utilities online that will rename files for you. Many have fancy bells and whistles that go along with the task. However, if you simply need to add a suffix to all files meeting some naming or extension or path criteria, the old-fashioned way is the quickest and easiest.

The best part is that your computer - whether Windows, Mac or Linux - already has the stuff to do it, built right in.

Well, that's not quite correct: the best part is that you'll learn a nifty trick doing it this way, and you'll get to wield a little more power than is afforded by your mouse.

The Options

The first and probably most popular solution is to find a file renaming thingy online, download it and use it to perform the task.

The second option is to use the tools that already exist on the computer to do it.

I figure, teach a man to rename, and he will

s/eat lunch/save files/g
for a lifetime. :)

The Fun Way for Windows Users

Say you have a bunch of files in a directory. You open a command prompt by hitting the Windows key and the letter 'R' at the same time, then type cmd in the dialog that pops up and click OK. In the command prompt window, you cd to your directory and ask for a listing, getting a result that looks something like this:

C:\Users\me>cd \Audio\Recording

C:\Audio\Recording> dir /b

FirstSoundFile.mp3
SecondSoundFile.mp3
ThirdSoundFile.mp3
FourthSoundFile.mp3
FifthSoundFile.mp3

The dir /b command simply means, "gimme the directory listing, but just the filenames - no dates, sizes or anything."

Great! The files are all there! But you need to add a suffix onto each one, after the main part of the filename, but before the extension. Following our real-world example, that suffix is to be _QRZ.

How many commands do you think are needed to get it done? How many lines would you guess you'd have to type in?

One. Yep, just one. It's what we call a one-liner. It looks like this:

for /f "tokens=1* delims=." %F in ('dir /b *.mp3') do rename "%F.mp3" "%F_QRZ.mp3"
Let's break it down, shall we?
for /f
I'm about to feed you a list of files - please look at each one in turn.
"tokens=1* delims=."
read one or more pieces of the name, delimited by a period character
%F in ('dir /b *.mp3')
Ask dir to get a list of files in our directory, reminding it with /b that we don't care about dates or sizes. For each whose extension is mp3, make a note of its name in a variable called F so we can diddle it in a moment
do rename
I want you to change the name of the file...
"%F.mp3"
from whatever it is right now...
"%F_QRZ.mp3"
...to pretty much the same thing, but stick _QRZ in just before the extension.

So you type that in, hit Enter and away it goes. How fast is it? Well, even for a big clump of files on a run-of-the-mill computer, it'll probably be done before you get your finger far off the Enter key.

The Result

After execution, your directory will look like this:
C:\Audio\Recording> dir /b

FirstSoundFile_QRZ.mp3
SecondSoundFile_QRZ.mp3
ThirdSoundFile_QRZ.mp3
FourthSoundFile_QRZ.mp3
FifthSoundFile_QRZ.mp3

The Final Step

Invoice the client. And get a cup of coffee, because you're thirsty from all this nonwork.

The Even Funner Way (*nix Only)

Open your terminal, cd to your directory, and say:

rename .mp3 _QRZ.mp3 *.mp3
For Linux (and, presumably, Mac OS users), this really isn't terribly exciting. rename, by the way, is a built-in, is not quite identical to mv, and on older distributions may actually be a perl script. Whatever the case for your flavor, it should work just fine.

Advanced Studies

Instead of pasting the command in a text file and pasting it into a command prompt each time you need to use it, you can turn it into a self-contained little thingy that will run when you double-click its icon. If you've never written a program before, it's okay if you want to think of this as your first little program written by you!

A few minor edits will make our renaming command work in what's called a batch file. Batch files are simply text files that have a bunch ("batch") of commands that are executed in order from top to bottom, one line at a time. This is a bit of a simplification - you can do some branching and other fun stuff, for example - but that's not important right now.

Along with the edits to make it batch-file friendly, it would be really keen if you could specify the target directory, file extension and suffix text each time you run it. That way, you can put the batch file somewhere - on the desktop, for instance - and use it to rename files anywhere on your computer.

Open up your text editor, copy the code below and paste it into the editor (Notepad will work fine for this). Note that, while the meat of the thing is essentially the same, some of the percent signs are doubled. This is necessary for it to work from within a batch file. Don't worry too much with the why, just believe me.

@echo off
set /p ext="File extension to look for: " %=%
set /p suf="Suffix to add: " %=%
set /p tdir="Target directory: " %=%
cd %tdir%
for /f "tokens=1* delims=." %%F in ('dir /b *.%ext%') do (
echo Renaming "%%F.%ext%" to "%%F%suf%.%ext%"
rename "%%F.%ext%" "%%F%suf%.%ext%"
)

echo All done!
pause
Once pasted, save the file as a .bat file and give it a name you like - say, renamer.bat.

In Notepad, you must change the file type selection to All Files before doing this; otherwise, Notepad will ever so helpfully stick a .txt extension onto your filename, and then it won't work.

After you've saved the file, run it by double-clicking its icon. After answering the prompts for extension, suffix and target directory, you'll see your files being renamed, then the "All Done!" message when the process completes. The pause at the end of the batch file ensures the command prompt window will remain on the screen after completion, prompting you to "Press any key to continue...".

This shot is what you'll see - in this case, I answered the prompts so that we'll look for all mp3 files in c:\Audio\Recording and add _ABC123 to their names.

At each prompt, simply type in what you want and then hit Enter.

Finally, here's what the files now look like, as seen in a normal Windows window:

And another thing...

You're not restricted to only using this for audio files - this will work on any kind of file you want to diddle.