Change the HOUR in filenames. Batch Script
I have my jpg filenames as EXIF dates. The format of the filename is YYYY-MM-DD-HH-MM-SS.JPG. This way it keeps them sequential at all times. Now开发者_JAVA技巧 my camera was set to the wrong hour. So how do I change the HH to -1 hour than it actually is.
For example, if a filename is "2010-11-26-19-15-17_C.jpg", I want to change it to "2010-11-26-18-15-17_C.jpg". I have hundreds of files, and they all need to be changed to one hour prior than what is actually in the filenames.
Maybe a batch script? I've used Renamer software, but I can't get it to work just right.
Note that in the worst case you might need to change not only the hour, but also the date, month and year as well (2010-01-01-00-10-10.jpg
for example will have to become 1999-12-31-23-10-10.jpg
)
If you ignore this caveat, here's the batch file that would allow you to decrement only the hour component of the name.
@echo off
setlocal
for /f "usebackq delims=|" %%f in (`dir /b *.jpg`) do call :ProcessFile %%f
goto :End
:ProcessFile
for /f "tokens=1,2,3,4,*delims=-" %%i in ("%1") do call :ParseName %1 %%i %%j %%k %%l %%m
goto :End
:ParseName
set /A NEW_HOUR=%5-1
if "%NEW_HOUR%" == "9" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "8" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "7" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "6" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "5" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "4" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "3" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "2" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "1" set NEW_HOUR=0%NEW_HOUR%
if "%NEW_HOUR%" == "0" set NEW_HOUR=0%NEW_HOUR%
set OLD_NAME=%1
set NEW_NAME=%2-%3-%4-%NEW_HOUR%-%6
echo Old name : %OLD_NAME%
echo New name : %NEW_NAME%
rem Do copy, rename or whatever you want to do
goto :End
:End
endlocal
Note that editing the file name does not affect the EXIF data stored inside the JPG file that includes this same incorrect time stamp.
A good way to deal with this problem in general is to use a tool that can modify the EXIF data, as well as rename the file to match. One such tool is Exiftool by Phil Harvey. It is free, open source, and portable to many platforms. It even supports Windows.
Quoting from its site:
Have you ever forgotten to set the date/time on your digital camera before taking a bunch of pictures? ExifTool has a time shift feature that makes it easy to apply a batch fix to the timestamps of the images (ie. change the "Date Picture Taken" reported by Windows Explorer). Say for example that your camera clock was reset to 2000:01:01 00:00:00 when you put in a new battery at 2005:11:03 10:48:00. Then all of the pictures you took subsequently have timestamps that are wrong by 5 years, 10 months, 2 days, 10 hours and 48 minutes. To fix this, put all of the images in the same directory ("DIR") and run exiftool:
exiftool "-DateTimeOriginal+=5:10:2 10:48:0" DIR
The example above changes only the DateTimeOriginal tag, but any writable date or time tag can be shifted, and multiple tags may be written with a single command line. Commonly, in JPEG images, the DateTimeOriginal, CreateDate and ModifyDate values must all be changed. For convenience, a shortcut tag called AllDates has been defined to represent these three tags. So, for example, if you forgot to set your camera clock back 1 hour at the end of daylight savings time in the fall, you can fix the images with:
exiftool -AllDates-=1 DIR
It also supports renaming files based on a file name template that can not only change the name, but also distribute files into a tree. There is a full example at the site, but the simple case looks like this
exiftool "-FileName<CreateDate" -d "%Y-%m-%d-%H-%M-%S.%%e" DIR
to rename every file in the directory named DIR
to match your requested template.
精彩评论