ExifTool Forum

ExifTool => Newbies => Topic started by: JJ_7 on January 26, 2023, 09:48:22 PM

Title: if statement to set tags from filename
Post by: JJ_7 on January 26, 2023, 09:48:22 PM
Hi people,

I have used ExifTool a tiny bit over the last 10 years (very occasionally), and regex a bit more with AdvancedRenamer that uses ExifTool. These are amazing tools! But this question has me stumped.... (btw I am using v12.55)

I have image files with the following types of filename format:

I would like a regex that uses "01" if the month or day digits are "00", to save me exporting to excel and converting there and running a command line for each file.

So far I used one of the following that works only for case 1:

aside: "-DateTimeOriginal<${Filename;m/(\d{8})/;$_=$1} 12:00:00+01:00" for time zone adjustment didn't seem to work.

I have unsuccessfully tried variations of the following:
"-DateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.$2.-if '$3 eq "00"' "01" -if '$3 ne "00"' $3} 12:00:00"
Assistance would be much appreciated.
Title: Re: if statement to set tags from filename
Post by: StarGeek on January 27, 2023, 12:08:46 AM
Quote from: JJ_7 on January 26, 2023, 09:48:22 PMI would like a regex that uses "01" if the month or day digits are "00", to save me exporting to excel and converting there and running a command line for each file.

I changed 197501... to 197503... to make a more obvious difference between 00 and a regular 01 date.

Here I used Perl's conditional operator (https://perlmaven.com/the-ternary-operator-in-perl)  to check to see if the matched value was equal to 0 and set it to '01' else keep the value.

C:\>exiftool -G1 -a -s -DateTimeOriginal Y:\!temp\dd
======== Y:/!temp/dd/19750000 My favourite place.jpg
======== Y:/!temp/dd/19750300 My favourite place.jpg
======== Y:/!temp/dd/19750327 My favourite place.jpg
======== Y:/!temp/dd/19750000c My favourite place.jpg
    1 directories scanned
    4 image files read

C:\>exiftool -P -overwrite_original "-DateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00" Y:\!temp\dd\
    1 directories scanned
    4 image files updated

C:\>exiftool -G1 -a -s -DateTimeOriginal Y:\!temp\dd
======== Y:/!temp/dd/19750000c My favourite place.jpg
[ExifIFD]      DateTimeOriginal                : 1975:01:01 12:00:00
======== Y:/!temp/dd/19750300 My favourite place.jpg
[ExifIFD]      DateTimeOriginal                : 1975:03:01 12:00:00
======== Y:/!temp/dd/19750327 My favourite place.jpg
[ExifIFD]      DateTimeOriginal                : 1975:03:27 12:00:00
======== Y:/!temp/dd/19750000 My favourite place.jpg
[ExifIFD]      DateTimeOriginal                : 1975:01:01 12:00:00
    1 directories scanned
    4 image files read

Even though I used numeric equal $2==0, it could just as easily been a string comparison $2 eq '00'.  This way is slightly shorter.


Quoteaside: "-DateTimeOriginal<${Filename;m/(\d{8})/;$_=$1} 12:00:00+01:00" for time zone adjustment didn't seem to work.

Are you trying to add 1 hour to the time?  Or do you want a +01:00 time zone?  If the latter, the EXIF time stamps do not include a time zone component.  That is kept in a separate tag:
EXIF:DateTimeOriginal -> EXIF:OffsetTimeOriginal
EXIF:CreateDate -> EXIF:OffsetTimeDigitized
EXIF:ModifyDate -> EXIF:OffsetTime

Exiftool does provide Composite tags which allow for the writing of both the main date/time tag and the time zone tag (as well as the matching sub-second tags) at the same time. These are
SubSecDateTimeOriginal
SubSecCreateDate
SubSecModifyDate
So you could use SubSecDateTimeOriginal instead of DateTimeOriginal to write the date/time and time zone.  See the Composite tags page (https://exiftool.org/TagNames/Composite.html).

Myself, I tend to use a wild card to set all three OffsetTime tags at once
-OffsetTime*=+01:00
On Mac/Linux, this would need to have single quotes around it because without it the asterisk becomes a list of file names.