Ryan Bagley Blog Tags


Using ExifTool to Set Dates Based on Filename

ExifTool has been a surprisingly useful piece of software in my recent quest to archive all of my family photographs. This includes scanned film from before the time of personal computers and also mangled files that don’t have the correct EXIF data to indicate exactly when they were taken. With the correct EXIF data you can maintain a tidy image archive that plays well with other programs, such as image editing or storage software.

According to user StarGeek, Google Photos will pick from the following tags to sort a recently uploaded image.

If Google Photos choses a field that isn’t exactly correct you have to edit the date and time manually. But in my experience, this doesn’t actually change the EXIF information on the image file, it just adds information to a separate .json file. So rather than depending on Google Photos or some other software I tasked myself with correcting this information in the file itself. Both for transportability and ease of mind. I can easily move to different storage systems or software without worry.

One things to keep in mind, as ExifTool author Phil Harvey has repeatedly stated, dates before 1970 and after 2038 give unpredictable results. There are ways around this limitation but approach with caution depending on your system platform.

I’ve established the need to update EXIF information correctly but how do you actually do it based on filename? The basic approach is outlined in ExifTool’s FAQ in step 5. But to ingest a filename as I needed, it required some further tweaking using more advanced logic with ExifTool’s advance formatting feature that utilizes Perl expressions.

The alldates command requires that all 6 date and time fields be present in some way in the filename like YYYYmmddHHMMSS. It can parse through dashes and some other non-numerical characters but the actual numbers will shape the date and time output exactly unless you tell it otherwise. For my files named 2019-05-15_0023.jpg I don’t have any hours, minutes, or seconds specified in the filename. The last four numbers are just a random sequence of numbers. If I run the command normally it thinks I’m specifying 23 minutes past midnight. To get around this I needed to use ExifTool’s advanced logic.

exiftool '-alldates<${filename;s/_.*//} 120000' 2019-05-15_0023.jpg
    1 image files updated

What this actually does is it instructs ExifTool to read the file name to obtain the year, month, and day in order to update EXIF date fields for DateTimeOriginal, CreateDate, and ModifyDate. Another thing it does is it manually assigns a time stamp of noon. The Perl expression of s/_ matches the portion before the underscore _ and everything that follows (.*) in the filename is replaced with nothing (//). The value of 120000 is interpreted in HHMMSS format, which ExifTool interprets as 12:00:00.

exiftool -alldates 2019-05-15_0023.jpg
Date/Time Original              : 2019:05:15 12:00:00
Create Date                     : 2019:05:15 12:00:00
Modify Date                     : 2019:05:15 12:00:00