ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: BravoCharlie on May 26, 2022, 06:26:58 PM

Title: extract datetime from filename, define time manually if not given
Post by: BravoCharlie on May 26, 2022, 06:26:58 PM
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 (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 (https://exiftool.org/forum/index.php?topic=6408.15)
- Setting DateTimeOriginal from YYYY & MM from filename + dummy day & time (https://exiftool.org/forum/index.php?topic=13188.0)
- insert generic datetime values if missing (https://exiftool.org/forum/index.php?topic=11981.0)
- Change date only (not time) based on filename (https://exiftool.org/forum/index.php?topic=11068.0)

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).
Title: Re: extract datetime from filename, define time manually if not given
Post by: Phil Harvey on May 27, 2022, 10:19:40 AM
This seems like a good way to do it.

- Phil