Thursday, August 21, 2014

Renaming Files with Batch

After the brief introduction to Batch Scripting, I think it's time to present another script with different application to reinforce the newly-introduced, powerful, and useful tool. Imagine that you have always ripped all your own music CD's and named them in the same way all along. Now your compilation has grown to a library of hundreds of songs named in the same pattern, say "XXX - YYY.mp3". Well, but now you have realized that you would prefer to name them all "YYY - XXX.mp3" instead, because this way works better for you, or it just makes more sense to you. If you pay close attention to organization like I do, then you would want to name all your MP3's consistently. But, would you rename each and all of the hundreds of files manually?

No.

Simplify your life: The Renaming Script

This is the case where this scripts is particularly useful, as it basically renames all the files within a folder by swapping 2 blocks of the name separated by a delimiter, "-". Basically, the script can change the names of all files within the working folder from "Songname - Artist.mp3" into "Artist - Songname.mp3" Of course, the script can be used as a skeleton and could be modified to adapt specific criteria.

Important: Please make sure that your computer is configured to Show File Extensions.
Recommendation: Test your modified script on a copy of your music database and not on the original one. So if something goes wrong, you would still have the original, untouched files.

In this particular script, we make use of the previously introduced Batch instructions such as the For loops, Echo, Pause, and Comodins. In this script, I will introduce Flow Control, String Manipulation, Functions, and Variables. Of course, I will not explain them in details as the purpose of this is to briefly describe and provide an overview of this script. Nevertheless, if you are not familiar with the aforementioned terms and are interested to learn more about them, then I suggest you Google them =D.

The Code

As customary, the green text means comment and will be ignored by the interpreter. The red text is the actual code that will be executed by the interpreter. Also, I have made the script create a log in temp.txt to keep track of what has been done. A log is always a good idea as it provides the results and helps tracking down faults.

The :F1 in the code is basically a tag representing a function while the do call :F1 actually calls it and executes it. The goto is flow control and quite self-explanatory. The variable var, in this case, simply stores a series of letters (a.k.a. String) which can be changed and manipulated. Naturally, if you have something that don't understand and would like to know, you can always Google it. Writing code requires your ability to research and to know where to find your answers.

::Turns an "Songname - Artist.mp3" into "Artist - Songname.mp3"
::for all *.mp3 inside the folder.

::Trims the first 5 letters for all the *.mp3
setlocal ENABLEDELAYEDEXPANSION
for %%A in (*.mp3) do call :F1 "%%A"
for %%b in (*.mp3) do (
set string=%%b
echo !string!
::Replace the " - " with something that
                ::will not appear such as ";"
set string=!string: - =;! 
echo !string!
::Trim the extension ".mp3"
set string=!string:~0,-5!
for /f "tokens=1,2 delims=;" %%c in ('echo !string!') do (
::Gets the new name now.
set newname=%%d - %%c%%~xb
echo !newname!
        ren "%%b" "%%d - %%c%%~xb">>temp.txt)
)
endlocal
pause
goto :CONT

:F1
::Always assign parameter to a new variable.
set var=%1
set var=!var:~6!
set var=!var:~0,-1!
echo !var!>>temp.txt
ren %1 "!var!"
exit /b
::Swaps the "Songname - Artist.mp3" into "Artist - Songname.mp3"
:CONT


Some Final Words

As before, you can Copy/Paste the code and save as "*.bat" file. Make sure that the script file is located in the same folder as all your MP3's. This will not work if the script file and the MP3's are not in the same folder. I reckon there was a lot of new and complex concepts, but you do not necessarily have to learn them. If you have programming/scripting background, you can try to understand the code and learn from it. Otherwise, I advise you to keep it as abstract as possible, unless you have the interest to learn.

Important: This script changes the names of your files, so use it cautiously.
You use this at your own risk as I will not be responsible for any damage you may cause.

As was mentioned, do not test on your original files. Work on the copy just in case something goes wrong. Use this at your own risk as I will not be held responsible for any damage this may cause. I hope that you use this script cautiously and improve on it. There is always room for improvement especially when this is not optimized at all. This script could be extended to handle more cases, modified to work with different filename extensions, or even used for other applications. I appreciate and welcome your improvements, questions, ideas, and comments.



No comments:

Post a Comment