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
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

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.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

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