Negative/Opposite -if Conditional?

Started by Stephen Marsh, September 23, 2016, 01:25:44 AM

Previous topic - Next topic

Stephen Marsh

I have a test file, it has an IPTC:Credit entry of: XXX

I have two questions that have me stumped (I have searched high and low without joy):

1) It appears that the -if condition can use a wildcard of either ? or * for any character, however these fail in my test. I do get a matching result with a period . though? What is going on with that (see CLI sample below)?

2) The following command will process all of the files with an entry in the -credit field... However what if I want files processed that DO NOT match the -if criteria? If this is not possible, then how do I use the -if condition to search for blank/empty values in a given field?

exiftool -if '$credit =~ /./'

So I tried the following for the reverse, but that did not have the desired result either!

exiftool -if '$credit !~ /./'


I am guessing that I probably have one character wrong or something simple...

What I am hoping to do is if the credit entry is blank missing, then populate a totally different field with a set value.

Stephen Marsh

#1
OK, I found the answer to my second question here:

https://exiftool.org/forum/index.php/topic,3411.msg15404.html#msg15404

It appears that I need to use the following to not match the if condition:

-if 'not defined $credit'

Which leaves unanswered the * ? . wildcards in my first question?

StarGeek


Quote1) It appears that the -if condition can use a wildcard of either ? or * for any character, however these fail in my test. I do get a matching result with a period . though? What is going on with that (see CLI sample below)?

The -if option uses perl language to evaluate the expression following the -if option.  In the examples you give, it's a Regular Expression (RegEx) match which is being evaluated.  In RegEx, the dot . is a wildcard which matches (almost) any character.  It is similar to the question mark in Windows CMD.  The Asterisk * is an quantifier which means match the previous token 0 or more times, taking as many characters as possible.  If you put the two together .*, it's similar to just the asterisk in the Windows CMD. 

The question mark is another quantifier, but only matches the previous token 0 or 1 times.  So if your expression is /colou?r/, it would match both color and colour, matching the u 0 or 1 times.

From there, it gets even more complex.  I used Regular-Expressions.info to learn about RegEx and I use RegEx101.com to test out RegExs and see a step by step breakdown of what's happening.


Quote2) The following command will process all of the files with an entry in the -credit field... However what if I want files processed that DO NOT match the -if criteria? If this is not possible, then how do I use the -if condition to search for blank/empty values in a given field?

exiftool -if '$credit =~ /./'

This expression will match any credit tag that has at least 1 character.

QuoteSo I tried the following for the reverse, but that did not have the desired result either!

exiftool -if '$credit !~ /./'

This one will match any tag that doesn't have any characters.  It will match a tag that exists but has nothing in it or a tag that doesn't exist.  For example, you could type:
exiftool -if '$BatTag!~ /./' -credit File
and it will output the value of the Credit tag, because BatTag doesn't match the dot.

QuoteWhat I am hoping to do is if the credit entry is blank missing, then populate a totally different field with a set value.

To find tags that are blank, try this command
Exiftool -if '$credit=~/(^\s*$)/ -Credit='New Value' FileOrDir
or
Exiftool -if '$credit=~/^\s*$$/ -Credit='New Value' FileOrDir

To break this down, the caret ^ is an anchor indicating the start of the tag.  The \s indicates a white space (space, tab, etc).  The asterisk is 0 or more of the previous, in this case the white space.  And the dollar sign anchors to the end of the line.  Now, because Exiftool will interpret the $/ as a line feed, I usually enclose the expression in parenthesis, but you can also double the dollar sign to escape it in this case.

I believe there's another way to do this as well, without the if option, but it always confuses me, so I'll leave it to someone else to mention it.
* 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).

StarGeek

Quote from: Stephen Marsh on September 23, 2016, 01:49:11 AM

It appears that I need to use the following to not match the if condition:

-if 'not defined $credit'

One thing to point out is that if the tag exists, but is empty, or if it is full of blank spaces, this will not match.

One possibility here would be -if 'not $credit'.  That will match files without a Credit tag and with a Credit tag that is empty or only has white spaces.  One side effect though, is that it will also match a credit tag that is '0'.

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

Stephen Marsh

Thank you StarGeek, you must have been researching/writing your reply while I was searching/replying to my OP!.

It is hard to know which I am more of a newb at – RegEx or ExifTool  :-\

Yes, I did put in the period hoping that this would be interpreted as a (regex) wildcard for any character, however I did not think to make it greedy!

In my simple test I get the same result with both, which is fine by me... However is one better than the other? It would appear that the second option that you mentioned would be safer than the first:

-if 'not defined $credit'

or

-if 'not $credit'

Thanks again mate!

StarGeek

Quote from: Stephen Marsh on September 23, 2016, 02:34:52 AMHowever is one better than the other?

The second one is more likely to grab more variations.  But it all comes down to how well you know the metadata in your files, what programs were used to process them and what those programs did. 

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