2006/11/06

Rename files to match (or include) their date/time stamp

This article is quite old but I find it really useful. I'm sure you have had such a need sometime and either you managed without it, circumventing the problem or solving it yourself (doing a little in-home application). The article is related to digital cameras and renaming the files in a more convenient way instead of the useless schemas that they use. However this can be applied to rename a backup file just after it has finished, using the date of the file so so that the next backup does not overwrites the old one or any other scenario that you can think about. Rename Files to Match Date/Time Stamp
My digital camera generates filenames like IMG001.jpg, IMG002.jpg, and so on. This isn't useful, because the filenames don't describe anything, and I often run into problems with duplicate filenames when I transfer images to my computer. Is there a simple way in Windows XP Professional to rename a group of files, with each filename made up of the date and time of file creation? For example, I'd like to see filenames such as 20030730_143222.
A sample that just echoes the names of the files in the current directory including the date/time just before the file's extension.
@ECHO OFF
FOR %%V IN (%1) DO ECHO %%V [%%~tV] %%~xV
And here the final command. In this case the original file name is completely discarded.
FOR %%V IN (%1) DO FOR /F "tokens=1-6 delims=/: " %%J IN ("%%~tV") DO IF EXIST %%L%%J%%K_%%M%%N%%O%%~xV (ECHO Cannot rename %%V) ELSE (RENAME "%%V" %%L%%J%%K_%%M%%N%%O%%~xV)
If you want to add date/time information before the extension and keep the oringinal name in place, you can use:
RENAME "%%V" "%%V-%%L%%J%%K_%%M%%N%%O%%~xV"
Instead the former rename.

No comments: