So a problem arose the other day that required me to change some file names for a user. Not a very difficult task (I thought), but once the user gave me a list of over 400 changes, I realized that this was a much bigger problem then originally assumed.
After weighing many options, I decided a script was probably my best bet. Now, while I specialized in programming in college, I am a complete neophyte when it comes to scripting. Thankfully, some folks over at Computing.net was there to guide me. I changed the user list into a 2-column CSV file. The first column had the current file names while the second had all of the new names. With this in hand (or on hard drive) I could start the automation structuring. The script had to do 3 unique things:
- Detect of the original file exists (output if it does not)
- Detect of the new file name already exists (do not override new files)
- Actually change the files that have good values
I was attempting to change pictures files (JPG), if you are changing a different time, be sure to remember to change the extension on the variables %%a and %%b. Here is the script file:
@echo off>rname.txt & setlocal enabledelayedexpansion
for /f “tokens=1-2 delims=,” %%a in (nameOfCSV.csv) do (
if exist %%a.jpg (
if not exist %%b.jpg (
::Remove the “::” from the next line when you are ready to commit the name changes
::ren %%a.jpg %%b.jpg
echo renamed %%a.jpg to %%b.jpg>>rname.txt
) else (
echo ::attempted duplicate: %%b already there>>rname.txt
) else (
echo ::no identity exists: %%a>>rname.txt
Simply save this script as a BAT file and move both it and the CSV file into the directory with the files you wish to change. The line that actually changes the files is commented out in the above code. The reason for that is that you can read the output to verify that everything is OK before committing any changes. When you are ready to change the files, just remove the “::” in the line ” ::ren %%a.jpg %%b.jpg”.
What I like most about this approach is that you can add values to the CSV file at your leasure and schedule the actual BAT file to run at a standard interval.