Sooner or later you may face the problem of how to change the names of a large group of files. A case in point is a situation wherein you have a group of files – perhaps a group of episodes from a TV series that have been named correctly, but in a non-standard format – and you need to change those file names to conform with an accepted standard, for example, in the form of SXXEYY where XX is the season number and YY is the episode number, followed by the file type suffix. This code is often referred to as the “Sexxyy” standard.
The original files may come from a variety of sources, many of them ad hoc and without regard for the standards, or perhaps named for a different standard (there are several to choose from).
Taking a download from the Internet, you might get a list of files that look like this:
BSG [1x01] 33 FASM HR HDTV.wmv
BSG [1x02] Water FASM HR HDTV.wmv
BSG [1x03] Bastille Day FASM HR HDTV.wmv
BSG [1x04] Act Of Contrition FASM HR HDTV.wmv
BSG [1x05] You Can’t Go Home Again FASM HR HDTV.wmv
BSG [1x06] Litmus FASM HR HDTV.wmv
BSG [1x07] Six Degrees Of Separation FASM HR HDTV.wmv
BSG [1x08] Flesh And Bone FASM HR HDTV.wmv
BSG [1x09] Tigh Me Up, Tigh Me Down FASM HR HDTV.wmv
BSG [1x10] The Hand Of God FASM HR HDTV.wmv
BSG [1x11] Colonial Day FASM HR HDTV.wmv
BSG [1x12] Kobol’s Last Gleaming – Part 1 FASM HR HDTV.wmv
BSG [1x13] Kobol’s Last Gleaming – Part 2 FASM HR HDTV.wmv
The next step is to change the names to conform with the standard.
Most users will be familiar with the Windows Explorer program and its ability to change single file names and some users will be aware of the ability to highlight a range of files and right-mouse-click to rename the selected files to a single “base name” – the result being a list that looks like filename.txt, filename (1).txt, filename (2).txt etc. but neither of these can be used in a batch file nor do they give us the kind of control we need over the resulting file names.
Fortunately, Windows continues to provide a command line interface — the “command prompt” as it is known in Windows 7.
Changing the name of a file can be done with several commands from the command line – “copy”, “rename” and the “move” command come to mind. Syntax for the move command is simply:
move “old file name.xxx” “newfilename.xxx”
Editing each file name individually is both error prone, and time-consuming. It’s much faster (especially if you plan to do more bulk changes in the future) to create a batchfile — better still if you create a batch file creator that will do the job for you.
Our first task is to create a list of the original file names. Open a command prompt and “cd” into the directory containing the video files then type the command “dir /B > C:\list1.txt”. This command will create a “bare” list of filenames like the list shown above and put that list into a file in the root directory of the C: drive called list1.txt.
NOTE: The file name and destination of list1.txt doesn’t matter so long as you can find it again.
Use excel to open C:\list1.txt and this will give you a worksheet with a list of the filenames in column A. Scan through the list to make sure the episodes are in the right order and that none are missing – if there are any missing episodes insert a blank cell in the list for each. Remove any files that are not videos in the series. The final list should be a complete list of all the episodes of one season.
Next, start the series of new SEXXYY episode file names for season 01 by entering S01E01 in column B1. Next, hover around the bottom right corner of cell B1 until the cursor turns into a thin dark cross-hair and drag the cursor down till it matches the bottom of column “A” creating consecutively numbered cells — S01E01, S01E02, S01E03 etc.
The third column is where the magic happens. It’s an excel formula:
=CONCATENATE(“move “”",A1,”"”",” “,”",” “,”"”",B1,”.avi”,”"”")
that says “combine the word “move” with whatever is the the first column (the existing file name) and whatever is in the second column (the sexxyy file name) and print the whole thing out like a stack of DOS command lines”.
A batch file is simply a file containing proper commands like the move command shown above – to execute the batchfile it’s best to give the file a “.bat” suffix as in “do_move.bat” then type the name of the batchfile at the command prompt. All that’s required is the name of the files to be moved, and the name of the file it is to be moved to. Where possible, it’s best to copy and paste the names from an existing list, or generate a sequence of names from a program like excel and then let the excel create the command lines.
Copy the list of commands in column “C” and paste them into a blank notepad page — using the notepad “save as” command, navigate to the folder containing the raw files and save the notepad page as a batch file with a name like “doShow.bat” – where “Show” is a short title for the TV Series.
NOTE: This version assumes all episodes are in the “.avi” format — if all episodes are in another format – like .mpg or .mkv , then replace “.avi” in the formula with the appropriate suffix. If your episodes are in different formats, use Batchfile2 below.
Using either the command prompt, or Windows explorer, navigate once again to the folder containing your target files and type or double click the command “doShow.bat” – list the files once again and they should now be the new names.
A fancier version of this spreadsheet requires an additional column to hold the file extension information.
Cells in C are =right(A1,4)
This says “Take the last 4 characters from whatever is in cell A1 (our file names). If the results don’t look right, check the contents of A1 for trailing blank spaces.
The last cell makes the command lines.
Cells in D are =IF((LEN(A1)>0),CONCATENATE(“move “”",A1,”"”",” “,”",” “,”"”",B1,C1,”"”"),”rem”)
Its purpose is 1) to replace blank lines with the word “rem” to avoid any stray characters fouling the script and 2) automatically add the right extension to the new file names. NOTE: this script is only good for 3-character extensions.
Here is what a final worksheet might look like:
The next step is to copy the cells in column D and paste them into a blank notepad page:
Save the batch file in the same folder as the original video files with a name like “doBSG.bat”.
Finally, use windows explorer or your command prompt to navigate to the target folder and execute (double-click) our batch file “doBSG.bat”. Refresh the folder view and you should see the new file names.