A Full Circle – Back To DOS for Renaming Files

A fairly common problem I face, are files that do not have a useful file-name. It’s quite common with MP3 and AVI files downloaded from not so great sources. Since many of these files are local to India, information about them is not easily available in resources such as freedb, amazon, imdb etc. Hence none of the automatic renaming solutions work.

If the file-names are however available on the web, it is a fairly simple matter to rename these nondescript files on your HDD. Just try:

for /f "tokens=1,2 delims=," %i in (fnames.txt) do ren %i.ext "%j.ext"

Mystified? Let me work backwards and explain it.

Your Wish Is My Command

What you see above is a Command Line operation. You need to type the above command in a Windows Command Prompt window and press Enter to execute it. Make sure you are performing this operation in the folder where your files are located.

For ex: if my MP3 files are located in My Music\SomeSongs folder, I will have to launch command prompt and then issue the following command to change the current working directory:

cd C:\Users\owner\Music\SomeSongs

The FOR command in Windows is a very versatile command that allows you to perform actions when GUI solutions for renaming files such as EasyTAG, Tag&Rename, MediaMonkey etc. have failed.

The /F command extension allows you to parse a file and use it’s output to modify the FOR action. The file from which input data wil be read is mentioned between ( ) brackets. In my case, it’s a text file called fnames.txt.

The text file is my database. In the text file, I have mentioned the current file-names and proposed file-names. The data is separated by comma.

If you opened my file in Notepad, it would look like:

T-001,Mukesh - Noori
T-002,Kishore Kumar - Barah Anna

You have probably figured out by now that I have a directory full of files called T-001.mp3, T-002.mp3 … T-101.mp3 and I want them renamed.

How did I create the fnames.txt file?

Step 1:

I visited the webpage where track information was present in a list (correctly ordered). I copied this information into Notepad. I cleaned up the notepad window so that all that was present was just the file-names. No other information such as Ads, Previous Page/Next Page links, extraneous spaces etc. I saved this file as webnames.txt

I also created a file called dirlist.txt that contained the names of the files in the folder. For this, use the command

dir /b /on>dirlist.txt

Actually in my case, I typed:

dir *.mp3 /b /on>dirlist.txt

What the command did was to only show the list of files with MP3 extension, just the Bare (/b) file-name (no filesize, date info) and sorted by in ascending order by name (/on). Instead of showing this list on the screen, it saved it to a file called dirlist.txt (>dirlist.txt) in the current working directory.

So I have two files – one containing the current file-names (dirlist.txt) and the other containing the proposed file-names (webnames.txt). I need to place this information side by side, separated by a comma.

Excel At Complicating Things

For this, I started Microsoft Excel. First I selected all the lines in the dirlist.txt file, copied them to the clipboard and pasted them into Column A of Excel. Then I copied all the lines from webnames.txt file and pasted them in Column B of Excel.

Excel figures out that you are pasting lines that are separated by newline character. As a result, it pastes each individual line in a row of it’s own, thus creating a record (row) with two fields (columns).

So now I have an Excel file with two fields. The fields contain side-by-side information that shows the current file-name and proposed file-name. I saved this file in Excel in the Comma Separated Value (CSV) and called it fname.txt. Now I have a database in plain-text format.

Note: Since I am using a command to separate the fields, I make sure that the file-names themselves do not contain a comma -or- all hell will break lose. You could put the planet and it’s inhabitants at risk!

Back to the FOR command

for /f “tokens=1,2 delims=,” %i in (fnames.txt) do ren %i.ext “%j.ext”

If you look at the command line, you will notice that I have mentioned my database file (fnames.txt), the delimeter as comma (delims=,). Using the tokens parameter, I have specified that I need the information from the database that is contained in Field 1 (current file-name) and Field 2 (proposed file-name).

The %i parameter asks the FOR command to create a variable called %i. Information from Field 1 of the database is placed in this variable. Since I am actually reading two fields, FOR command automatically creates another variable %j to hold information from Field 2.

The DO parameter of FOR command contains the action that FOR command will execute repeatedly. In my case, it’s a simple file rename operation. Rename a file called %i.mp3 to %j.mp3

During execution, %i and %j are replaced with data and you will see the command being executed is:

ren T-001.mp3 "Mukesh - Noori.mp3"

You will note that I have replaced .ext with MP3 since that is the extension of my files. Also note the double-quotes around “%j.ext”. This ensures that in case your proposed file-name contains spaces, the REN command treats it as a single file-name and not as other non-existent files.

Congratulations to me! I have spent an hour researching and figuring this out to rename 100 files when I could have just renamed them manually using copy-paste from the Browser into Windows Explorer in about the same time!

But then again, the goal was to rename 100 files in 2 minutes from next time on; 2 minutes is what it will take next time on!

Further Actions

The file-naming saga did not begin at this step, neither it ended at this step. For ex: My files were not called originally T-001.mp3, T-002.mp3 … They had pretty weird names. All that was correct was the track number. I used a MP3 Renamer to rename the file according to track number.

Once the renaming is successful, the files may now have meaningful names but the ID3 info still contains crap. Again, I used the MP3 renamer to derive Artist – Song information from the file-name and save the ID3 tag.

I might use this technique to rename .avi files (TV Series), .kar files (Karaoke collections) etc.

A reason why I even bothered to go to the command prompt was because the GUI tools did not work for me. Tools such as Tag&Rename, MediaMonkey do not support renaming files from Text files. OpenSource project EasyTag does – but for some reason, on my Win7 computer it just refused to work well. It kept crashing.

Command prompt to the rescue after all. Indeed, a classical return to home in a full circle.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.