News:

2023-03-15 Major improvements to the new Geolocation feature

Main Menu

Check for existence or non-existence of a tag

Started by stan, May 23, 2015, 03:19:29 PM

Previous topic - Next topic

stan

I know this is a simple question but can't figure out whether I'm doing things right. Is -if "$TagName" and -if "NOT $TagName" the proper way to check for the existence or non-existence of a tag?

StarGeek

I would go with -if "defined $TagName" or -if "not defined $TagName".  There are some specific cases in your example where you will get a inaccurate result.  For example, if the tag exists, but is empty, or if it's equal to 0, the condition is equal to false and will not be caught by your statement.

For example:
c:\>exiftool -P  -overwrite_original -xpcomment="0" X:\!temp\test3.jpg
    1 image files updated

c:\>exiftool -if "$xpcomment" -xpcomment X:\!temp\test3.jpg
    1 files failed condition

c:\>exiftool -if "defined $xpcomment" -xpcomment X:\!temp\test3.jpg
XP Comment                      : 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).

stan

Quote from: StarGeek on May 23, 2015, 04:32:44 PM
I would go with -if "defined $TagName" or -if "not defined $TagName".  There are some specific cases in your example where you will get a inaccurate result.  For example, if the tag exists, but is empty, or if it's equal to 0, the condition is equal to false and will not be caught by your statement.

Interesting, so -if "$TagName" will always fail when the tag: i) doesn't exist, ii) exists but is empty and iii) exists but is equal to 0? Can this behavior be relied upon?

StarGeek

Quote from: stan on May 23, 2015, 09:13:57 PMCan this behavior be relied upon?

It's how Perl handles boolean values, so pretty much.  Here's a page with some more examples.
* 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).

Phil Harvey

One trick I learned the hard way:  The "equal to 0" part is a string "eq", not a numerical "==".  So "0" is false, but "0.0" is true.

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