Having trouble with `-if` and an array

Started by thany, July 25, 2022, 01:14:01 PM

Previous topic - Next topic

thany

I'm trying to have exiftool only work on files that have their creator tag match a (pre-existing) array of creators. In the documentation it says "a Perl-like logic expression", which is good and well, but I don't know Perl at all. I tried a couple of variations I found on SO:


exiftool -R -XMP:Creator -if "$Creator ~~ ['creator_a', 'creator_b']" .
exiftool -R -XMP:Creator -if "grep($Creator, ['creator_a', 'creator_b'])" .
exiftool -R -XMP:Creator -if "any(['creator_a', 'creator_b']) eq $Creator" .
exiftool -R -XMP:Creator -if "['creator_a', 'creator_b']->contains($Creator)" .


The 2nd variety works on every file, all the others don't match anything. These are oneliner ways I found on the internet to see if an array contains a value. It's strange to me that exiftool expects Perl, but then valid Perl (according to the internet at least) doesn't appear to work as intended... Maybe I'm missing something.

To prove that the creators I need it to work on, exist, this works fine:


exiftool -R -XMP:Creator -if "$Creator eq 'creator_a'" .
exiftool -R -XMP:Creator -if "$Creator eq 'creator_b'" .


But I'd like to do this in one command if at all possible.

(PS: don't mind the `-XMP:Creator` bit, that's bollocks of course, but it's just temporary to get something to show up)

Phil Harvey

> exiftool a.jpg -xmp:creator -if "$creator eq 'creator_a, creator_b'"
Creator                         : creator_a, creator_b


(I ran this on Mac but have exchanged the quotes in the command above to use Windows quoting.)

- Phil
...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 ($).

StarGeek

Also, from FAQ #17 (emphasis mine)
      These tags may contain multiple items which are combined into a single string when reading.

The easiest thing to do when checking to see if a creator is part of that list would be to use a regex match
exiftool -if "$Creator=~/creator_a|creator_b/" -Creator /path/to/files/

Things can get a bit trickier if you need an exact match, especially if there's a matching substring.  In this case I'll add the -sep option to have a unique separator.  Plus you also have to take into account beginning and ending anchors
exiftool -if "$Creator=~m/(?:##|^)creator_a(?:$|##)/i" -Creator /path/to/files/

But my favorite way is the use the -api Filter option and compare the filtered output to the original, though it may not be best in this case.  Using the Filter option removes the extra checks needed with the -sep option.  I mostly use this when I need to do batch replacements rather than listing data.
exiftool -api "Filter=s/^creator_a$//i" -if "$Creator ne $Creator#" -Creator# /pat/to/files/
* 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).