Author Topic: Multiple -if, regex and substitution (s///)  (Read 4161 times)

orax

  • Jr. Member
  • **
  • Posts: 19
Multiple -if, regex and substitution (s///)
« on: June 04, 2014, 01:51:10 PM »
Hello, I would like to write the tag IPTC:Caption-Abstract only when necessary.

So, I test... if $IPTC:Caption-Abstract equal ${XMP:HierarchicalSubject;s/.*/hehehehe/}

Notes : commands lines 1-3 and 6 are only for information.
Tested on Windows 7.

exiftool.exe -XMP:HierarchicalSubject IMG.jpg
Code: [Select]
> Hierarchical Subject            : a|b|c   
exiftool.exe -IPTC:Caption-Abstract IMG.jpg
Code: [Select]
> Caption-Abstract                : zzzzzzzzzzz   
exiftool.exe -p "${XMP:HierarchicalSubject;s/.*/hehehehe/}" "IMG.jpg"
Code: [Select]
> hehehehe   
Do NOT work. Why ?
exiftool.exe -overwrite_original_in_place -if "$XMP:HierarchicalSubject =~ /a\|b\|c/" -if "$IPTC:Caption-Abstract ne ${XMP:HierarchicalSubject;s/.*/hehehehe/}" -IPTC:Caption-Abstract="hehehehe" "IMG.jpg"
Code: [Select]
> 1 files failed condition   
But here, it's ok.
exiftool.exe -overwrite_original_in_place -if "$XMP:HierarchicalSubject =~ /a\|b\|c/" -if "$IPTC:Caption-Abstract ne 'hehehehe'" -IPTC:Caption-Abstract="hehehehe" "IMG.jpg"
Code: [Select]
> 1 image files updated   
exiftool.exe -IPTC:Caption-Abstract IMG.jpg
Code: [Select]
> Caption-Abstract                : hehehehe   
Thanks for help. :-[

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16885
    • ExifTool Home Page
Re: Multiple -if, regex and substitution (s///)
« Reply #1 on: June 04, 2014, 02:19:16 PM »
Interesting.

You are using an undocumented feature.  While I knew that it was possible to use the advanced formatting feature in a -if expression, but I haven't documented or tested this because I didn't think that anyone would want to do it.

The problem is that internally there is only one value computed for a given tag in the -if expression.  This is a dilemma if you use the same tag more than once with different advanced formatting for each.

I still don't know why you want to do this (or even whether that is really what you want to do), but I can easily add the ability to allow this.

Look for this update in version 9.64.

- Phil

Edit:  Hmmm.  Technically it may be allowed by the documentation because note 3 for the -if option says: "Tags in the string are interpolated the same way as with -p".
« Last Edit: June 04, 2014, 02:34:11 PM by Phil Harvey »
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

orax

  • Jr. Member
  • **
  • Posts: 19
Re: Multiple -if, regex and substitution (s///)
« Reply #2 on: June 05, 2014, 03:06:11 PM »
I)
Quote
I still don't know why you want to do this
When I do a backup or a 2-ways synchro, the program compare the modification date/time of each files. So, modification time is important for me.

If I use -P or -preserve ("Preserve date/time of original file"), modification time will NEVER change.
Else without -preserve, modification time will ALWAYS change.

Examples for try to explain my problem :

# Get Comment tag, modified date/time ; and MD5 checksum
exiftool -Comment IMG.jpg && dir /T:W IMG.JPG  && \cygwin\bin\md5sum.exe IMG.jpg
Code: [Select]
Comment                         : oldComment
05/06/2014  17:14            69 557 IMG.jpg
0b7f6f780929780b8661d3d77b18c73f *IMG.jpg

# Set new comment...
exiftool -Comment=test IMG.jpg && dir /T:W IMG.JPG && \cygwin\bin\md5sum.exe IMG.jpg
Code: [Select]
    1 image files updated
05/06/2014  17:16            69 548 IMG.jpg # Comment tag is updated so date/time and size are changed, good
19c0584d936d02e188d19fdf72a327ed *IMG.jpg # MD5 changed, good

# Set the same comment again...
exiftool -Comment=test IMG.jpg && dir /T:W IMG.JPG && \cygwin\bin\md5sum.exe IMG.jpg
Code: [Select]
    1 image files updated # updated again... Why ? not need !
05/06/2014  17:17            69 548 IMG.jpg # *** date/time changed again *** NOT good
19c0584d936d02e188d19fdf72a327ed *IMG.jpg # MD5 not changed, good, because Comment tag is the same so file is not changed

The file does not change (same MD5) but "modified date/time" change. I think it's not logic. :o


II)
exiftool -Caption-Abstract -Comment IMG.jpg
Code: [Select]
Caption-Abstract                : CAPTION
Comment                         : zzz

# (Not very important) Verification with one -if. Conditions are ok, each -if return true.
exiftool -if '$IPTC:Caption-Abstract =~ /CAPTION/' -IPTC:Caption-Abstract='CAPTION' 'IMG.jpg'
    1 image files updated
exiftool -if '$Comment ne ${IPTC:Caption-Abstract;s/.*/XXXXX/}' -IPTC:Caption-Abstract='CAPTION' 'IMG.jpg'
    1 image files updated


# FAIL (nothing new here)

# $IPTC:Caption-Abstract and $IPTC:Caption-Abstract
exiftool -if '$IPTC:Caption-Abstract =~ /CAPTION/' -if '$Comment ne ${IPTC:Caption-Abstract;s/.*/XXXXX/}' -IPTC:Caption-Abstract='CAPTION' 'IMG.jpg'
    1 files failed condition

# Caption-Abstract and Caption-Abstract
exiftool -if '$Caption-Abstract =~ /CAPTION/' -if '$Comment ne ${Caption-Abstract;s/.*/XXXXX/}' -IPTC:Caption-Abstract='CAPTION' 'IMG.jpg'
    1 files failed condition


# But these seem to WORK

# $IPTC:Caption-Abstract and Caption-Abstract
exiftool -if '$IPTC:Caption-Abstract =~ /CAPTION/' -if '$Comment ne ${Caption-Abstract;s/.*/XXXXX/}' -IPTC:Caption-Abstract='CAPTION' 'IMG.jpg'
    1 image files updated

# Caption-Abstract and $IPTC:Caption-Abstract
exiftool -if '$Caption-Abstract =~ /CAPTION/' -if '$Comment ne ${IPTC:Caption-Abstract;s/.*/XXXXX/}' -IPTC:Caption-Abstract='CAPTION' 'IMG.jpg'
    1 image files updated


III)
Another strange thing. In these examples /[a-zA-Z]+0/ don't match BillGates0 but /[a-zA-Z]+/ match it.

exiftool -Copyright IMG.jpg
Copyright                       : BillGates0

exiftool -if '$Copyright =~ /[a-zA-Z]+0/' -if '"wwww" ne ${Copyright;s/.*/X/}' -Copyright='BillGates0' IMG.jpg
    1 files failed condition ???

exiftool -if '$Copyright =~ /[a-zA-Z]+/' -if '"wwww" ne ${Copyright;s/.*/X/}' -Copyright='BillGates0' IMG.jpg
Copyright                       : BillGates0
    1 image files updated :o

And now, with the same regex /[a-zA-Z]0/ but without the second -if : it's ok, it match BillGates0 !
exiftool -Copyright IMG.jpg && exiftool -if '$Copyright =~ /[a-zA-Z]0/' -Copyright='BillGates0' IMG.jpg
Copyright                       : BillGates0
    1 image files updated


orax

  • Jr. Member
  • **
  • Posts: 19
Re: Multiple -if, regex and substitution (s///)
« Reply #3 on: June 24, 2014, 02:14:21 PM »
As I tried to explain it, I needed to have a new "modified date/time" only if file is modified. It was essentially for the backup program.
To do this I use an -if test (since v9.64, it works). But I'm surprised to not find an option in help file to do this more simply (I mean to replace the test -if "$UD_XMPHierarchicalSubject ne $IPTC:Caption-Abstract" which verify if to write tag is necessary). I found -writeMode but it's not exactly what I want.

Also, in my case, I constate better performances if tags are overwritten only if necessary: 10x to 25x !
I run the exiftool command on my pictures regulary but only few of them are changed or added. So only few pictures need to be updated with exiftool.

I wanted to test with and without -if "$UD_XMPHierarchicalSubject ne $IPTC:Caption-Abstract":
For the test, suppose all pictures in directory "2013" have already a Caption-Abstract equal to UD_XMPHierarchicalSubject tag. Then, I add 1 new picture (with no Caption-Abstract) and 1 picture is modified (Caption-Abstract changed). So, just 2 need to be modified.

60 seconds to finish
exiftool.exe -config config.txt -overwrite_original_in_place -recurse -if "$UD_XMPHierarchicalSubject" -IPTC:Caption-Abstract<UD_XMPHierarchicalSubject 2013
> 51 directories scanned   437 image files updated   1 image files unchanged   


6 seconds
exiftool.exe -config config.txt -overwrite_original_in_place -recurse -if "$UD_XMPHierarchicalSubject" -if "$UD_XMPHierarchicalSubject ne $IPTC:Caption-Abstract" -IPTC:Caption-Abstract<UD_XMPHierarchicalSubject 2013
> 51 directories scanned   435 files failed condition   2 image files updated   1 image files unchanged   


In another test, with bigger files (*.RW2), second test runs +25x faster. ;D It's a good thing !

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16885
    • ExifTool Home Page
Re: Multiple -if, regex and substitution (s///)
« Reply #4 on: June 24, 2014, 02:19:28 PM »
Right.  I understand, thanks.  I'm glad 9.64 works for you now.

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