extract datetime from filename, define time manually if not given

Started by BravoCharlie, May 26, 2022, 06:26:58 PM

Previous topic - Next topic

BravoCharlie

I want to share with you my current solution for the following situation (in first tests it seems to work the way I want):
I have two types of image files without datetime exif data, but datetime information defined in the filename (1. with time information, 2. without). I want to write the datetime to exif and in case of missing time in the filename, I want to define it manually (here 00:00:00):

1. yyyymmdd_HHMMSS_optionalDescription001.jpg -> EXIF: yyyy:mm:dd HH:MM:SS
2. yyyymmdd_optionalDescription002.jpg                   -> EXIF: yyyy:mm:dd 00:00:00

By using -alldates<filename only, exiftool interpreted the occasional counter after the picture description as time. So this is what I came up with:

exiftool '-alldates<${filename;s/^(\d{8}).*/$1/} ${filename;s/^\d{8}_(\d{6}).*/$1/ or $_=000000}' -if '$filename=~/^\d{8}\D.*/' DIR

Basically I'm defining the date string based on the first 8 digits in the filename and then the time string based on the following 6 digits only if the filename matches the ^\d{8}_\d{6} pattern. If it doesn't I'm simply writing the string manually (I used a 'workaround' $_=undef if /\./ before, to catch the case if the filename did not match the regex expression (I wanted to check for the . of the file extension, since it returned the full filename in this case), but than i came across the use of OR in https://exiftool.org/forum/index.php?topic=8456.15). And finally another if-statement in the end, just to be sure - but not really necessary.

See also the following topics, which are similar but quite the same or do not answer my specific question:
- Extract original time and date data from part of filename
- Setting DateTimeOriginal from YYYY & MM from filename + dummy day & time
- insert generic datetime values if missing
- Change date only (not time) based on filename

Please feel free to give feedback/share your opinion on my solution - or give me a hint if this problem was already answered (couldn't find anything though).

Phil Harvey

...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).