if statement to set tags from filename

Started by JJ_7, January 26, 2023, 09:48:22 PM

Previous topic - Next topic

JJ_7

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:
  • 19750127 My favourite place.jpg
  • 19750100 My favourite place.jpg  'signifies Original Date Taken was in January 1975
  • 19750000 My favourite place.jpg  'signifies Original Date Taken was in 1975
  • 19750000c My favourite place.jpg 'signifies Original Date Taken was circa 1975.

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:
  • "-DateTimeOriginal<${Filename;m/(\d{8})/;$_=$1} 13:00:00"
  • "-DateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.$2.$3} 13:00:00"

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.

StarGeek

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  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.

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.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

JJ_7

#2
Thank you, Star Geek. This was spot on. Much appreciated.

exiftool -P -overwrite_original -L "-SubSecDateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00.00 +01:00" "C:\temp\*.jpg"

Or the following where the date info is not at the beginning of the file name

exiftool -P -overwrite_original -L "-SubSecDateTimeOriginal<${Filename;m/.*(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00.00 +01:00" "C:\temp\*.jpg"

The -L was needed. Related to European accents. Unsure why, exactly. Maybe my win10 uses utf16 not utf8. Hopefully I won't need African letters such as ɗ.

StarGeek

Quote from: JJ_7 on December 29, 2023, 01:15:27 AMThe -L was needed. Related to European accents. Unsure why, exactly. Maybe my win10 uses utf16 not utf8. Hopefully I won't need African letters such as ɗ.

This is FAQ #18.  By default, Windows uses cp437.  That FAQ has some options that might help with accented characters beyond what can be fixed the -L option.

Myself, I could never get any of that to work. I had to use this StackOverflow answer. Unfortunately, it also has the side effect of changing the fonts of some older programs and causing problems with their GUIs. If you use that option, you'll have to test it and see if the problems are acceptable.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).

JJ_7

#4
Thank you, StarGeek.

The behaviour seems a bit different in various versions of Exiftool and exiftoolGUI, unless my PC settings have changed.

Testing details as follows:

  • test file name "20210000c ô é ɗ Ɓ ɓ Ɗ Ɦ ɦ Ŋ ŋ õ.jpg"
  • Exiftool 12.72
  • ExiftoolGUI 6.27

All the following apply without "chcp 65001" i.e. with default CMD window settings. And Ɦ was visible unless noted otherwise.

1. My CMD window displays all characters OK for "DIR", without doing chcp 65001. The exception is Ɦ which did not display regardless of "chcp 65001", which is not important.

2a. The following works in the CMD window, with or without "-charset filename=UTF8":

Exiftool -P -overwrite_original -charset filename=UTF8 "-Description<${Basename;m/(.*)$/;$_=$1}" "-Subject<${Basename;m/(.*)$/;$_=$1}" "-Title<${Basename;m/(.*)$/;$_=$1}" *.jpg
2b. Similarly, the following works in ExifToolGUI direct command line, with or without the charset command

-P -overwrite_original -charset filename=UTF8 "-Description<${Basename;m/(.*)$/;$_=$1}" "-Subject<${Basename;m/(.*)$/;$_=$1}" "-Title<${Basename;m/(.*)$/;$_=$1}"
2c. in GUI the following (with -L) threw up an error and did not work

-P -overwrite_original -L "-Description<${Basename;m/(.*)$/;$_=$1}" "-Subject<${Basename;m/(.*)$/;$_=$1}" "-Title<${Basename;m/(.*)$/;$_=$1}"
by "works" I mean that the characters are correctly visible in
a. Windows Explorer right click properties details
b. metadata viewed in GUI

3a. The following worked from CMD window, with or without -L:

exiftool -P -overwrite_original "-SubSecDateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00.00 +01:00" *.jpg
exiftool -P -overwrite_original -L "-SubSecDateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00.00 +01:00" *.jpg
3b. in the GUI, the following worked (without the -L):

-P -overwrite_original "-SubSecDateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00.00 +01:00"
3c. in the GUI, the following did not work with the -L

-P -overwrite_original -L "-SubSecDateTimeOriginal<${Filename;m/(\d{4})(\d{2})(\d{2})/;$_=$1.($2==0?'01':$2).($3==0?'01':$3)} 12:00:00.00 +01:00"
4a. Export data to txt in GUI worked OK (one txt file per jpg file) (as viewed in Notepad)

4b. in the GUI the following did not work i.e. did not create csv file (as viewed in Notepad or Excel 365)

-n -a -all -csv *.jpg >metadata.csv
4c. in CMD the following initially created junk characters for European and other Unicode (i.e. all non ASCII characters) (as viewed in Notepad or Excel 365). The file created could not be viewed correctly even when opened in Notepad using UTF 8 encoding. ANSI other than ASCII were "�" and Unicode other than ANSI were "?"

However, when I tried again after a few hours, the resulting csv file displayed OK in Notepad, and could be imported OK into Excel 365 (Data tab, insert from external file), but did not display correctly when opened with Excel; this was an encoding issue when opening the file, unrelated to Exiftool.

exiftool -n -a -all -csv *.jpg >metadata.csv
exiftool -n -a -all -csv -charset filename=UTF8 *.jpg >metadata.csv
4d. in CMD the following created the expected European characters (ANSI) but not other Unicode characters

exiftool -n -a -all -csv -L *.jpg >metadata.csv

In summary:
1. Codes to write information to the picture files and view the information in the GUI seems to work for all tested characters
2. From GUI, exporting the metadata to individual txt files as viewed in Notebook worked
3. From CMD, exporting the metadata to csv (as viewed in Notepad or Excel 365) largely worked:
  • It initially did not work for UNICODE with UTF-8 specified or charset not specified (non-ASCII characters were displayed incorrectly) but later started displaying OK, albeit with a work-around for Excel that seems unrelated to ExifTool
  • From CMD, exporting European characters (all ANSI perhaps?) but not other tested Unicode characters were processed correctly with the -L option when exporting the metadata to csv (as viewed in Notepad or Excel 365), as expected.
4. Warnings when using the CMD window do not always display Unicode filename character correctly, even when the command applies the metadata correctly and even when "DIR" displays the file names correctly.

So the latest versions work well with a bit of unexplained behaviour at times. Well done and thank you.

FrankB

Quote from: JJ_7 on December 31, 2023, 09:48:59 PMSo the latest versions work well with a bit of unexplained behaviour at times. Well done and thank you.

Starting with version V6.2.0 ExifToolGui did change with respect to 'international characters'.
- All commands are first written to an args file using UTF-8 encoding.
- This Args file always contains:
-CHARSET
FILENAME=UTF8
-CHARSET
UTF
That solved a lot of issues.

BTW: If you open the 'Log Window' you can see exactly what GUI does, and even create a CMD, or PowerShell script

https://github.com/FrankBijnen/ExifToolGui/blob/main/Docs/changelog.txt

Edit: Also check this post. It also makes clear that using an args file works better for international characters.
https://exiftool.org/forum/index.php?topic=15481.0