I ran across a situation where I need to change the extension on 195 files. They all were names of the format <filename>.wmv.wma. Well, being the lazy person I am, I decided to take the time that would be required to change the names and automate it. Cool thing was I found out a way to do this by writing a batch script with about 9 lines of code. Before you get impressed, I need to give a disclaimer that I found the idea
here and tweaked it for my needs. Be sure to play with this using files that don't matter first.
So here's what I did.
1. I made a test folder and just made a bunch of files by copying and pasting.
2. In that folder I made a text document, and renamed it to be a .bat extension.
3. I edited that file with the following contents.
:: renamer.bat
@echo off
:main
for %%f in (*.wma) do call :renamer "%%f"
goto :eof
:renamer
set old=%1
ren %1 %old:~0,-5%
:: end
4. I had to do a little research to bone up on
string manipulation, and on
dos batch functions.
5. From a command prompt I ran the batch file so I could see errors.
There are 3 key portions of the script to note.
A. The first line in the main function uses a for loop searching for files. So the (*.wma) finds all files with a .wma extension. You will probably need to tweak this to match your files you want. Note if you just use a wildcard, you will also change the name of the batch file, so that could be a pain.
B. Spaces in the filename can do weird things. I have added quotes to the filename parameter to keep error messages from showing up.
C. The rename line (ren %1, %old:~0,-5%) runs the rename command with the function parameter as the old file name and a manipulation of the filename to be what is necessary. In this case, I want to remove the right 5 characters from the filename. So if %1 is "myfile.wmv.wma", I want to remove the .wma" and that is how I arrive at 5 characters. The expression %old:~0,-5% takes off the right 5 characters from the value of %old.
Well, this might be a little cryptic, but if you take the 30 seconds times the number of files you need to rename, it might be worth trying this out.