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?
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
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?
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 (http://perlmaven.com/boolean-values-in-perl) a page with some more examples.
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