Rename Files to Match Date/Time Stamp (1)


Bob Roberts

A batch file using the FOR command can do the job in one very long string. The FOR command was designed to apply a specific command to all files matching one or more file specifications. For example, the command can copy all JPEG files in the current folder to a new name starting with the word backup:

"FOR %%M IN (*.JPG *.JPEG) DO COPY %%M backup%%M"

The FOR variable, defined immediately after the word FOR, can be any uppercase or lowercase letter preceded by a pair of percent signs. Every occurrence of the variable in the DO portion of the command is replaced in turn by each filename matching the file specifications.

Rename Files to Match Date/Time Stamp (2)That's about all the FOR command does in DOS or Windows 9x, but the Windows 2000/XP version is vastly more powerful. Among other things, it can supply each file's date/time stamp and file extension, both of which are used in building the new filenames. We'll start with a demonstration of this capability. Use Notepad to create a file called Datename.bat containing just these two lines:


Pass a file specification to this batch file by entering DATENAME *.JPG, for example, at the command prompt. It will display each matching filename followed by its date/time stamp in brackets and then its extension. This works because the Windows 2000/XP FOR command allows a number of qualifiers in the FOR variable that yield different results. Inserting the qualifier ~t before the variable letter gets the date/time stamp in a format such as 05/16/03 09:54a. Inserting ~x returns the file extension, including the initial period. That's the information we'll need to build the new filenames, but the date/time stamp requires further processing.

Used with the /F command line switch, the Windows 2000/XP FOR command parses lines of text into individual words. If the value in parentheses is a filespec, FOR parses each line of each matching file. If it's a literal string (set off in double quotation marks), FOR parses just that string, which is what you need.

By default, the parsing function simply returns the first blank-separated word from each line, replacing the FOR variable with that word. But adding a tokens= qualifier makes FOR /F auto-create additional variables to hold multiple words, and a delims= qualifier defines additional characters as word separators.

We'll try it first with a hard-coded date/ time string. Replace the second line in your batch file with this line:

FOR /F "tokens=1-5 delims=/: " %%J IN ("05/16/03 09:54a") DO ECHO %%J %%K %%L %%M %%N

The command retrieves the first five words from the string, using the characters slash, colon, and space as word separators. %%J is the defined FOR variable; %%K through %%N are generated automatically.

The output of this command looks like this: 05 16 03 09 54a. The filename format that you want to employ with your image files is ordered by year, month, and day, with no spaces, so edit the command to end like this:

DO ECHO %%L%%J%%K_%%M%%N

Now the result looks like 030516_0954a, which would be suitable for a date/time– based filename.

The next step is to combine the two techniques, putting each file's date/time stamp in place of the hard-coded date and creating new filenames based on the data returned. Remember, this is still a single command line in your batch file:

FOR %%V IN (%1) DO FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO ECHO Rename "%%V" %%L%%J%%K_%%M%%N%%~xV

When you pass a file specification to this batch file, it will report all matching files along with the corresponding date-based filename. After you've verified that the command works, simply delete the word ECHO to switch its function from reporting the filename changes to making them.

There's one last complication. If two files have exactly the same date/time stamp, attempting to rename the second one will cause an error and halt the processing. Unless you can guarantee that each file will have a unique date/time stamp, you'll need to avoid this possibility. Thus we reach the final amazingly long command:

FOR %%V IN (%1) DO FOR /F "tokens=1-5 delims=/: " %%J IN ("%%~tV") DO IF EXIST %%L%%J%%K_%%M%%N%%~xV (ECHO Cannot rename %%V) ELSE (Rename "%%V" %%L%%J%%K_%%M%%N%%~xV)

With that, the batch file is complete.

Some readers noted that the batch file as written doesn't distinguish between AM and PM. This is true, but it's easily fixed. We need to request six tokens rather than five ("tokens=1-6") and insert the variable %%O wherever AM/PM is wanted. The modified batch file line looks like this:
