Author Topic: Syntax of the if-statement  (Read 18322 times)

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Syntax of the if-statement
« on: July 07, 2011, 02:58:07 PM »
Hello,

please excuse my dumb question, but my last program was some decades ago, and it was not written in ExifToolish. ;-)

I'm just wondering about the syntax of the -if condition.

A) In http://www.exiftool.org/exiftool_pod.html I found:


# add one hour to all images created on or after Apr. 2, 2006
    exiftool -alldates+=1 -if '$CreateDate ge "2006:04:02"' dir

A1: Why does the "if" stand after the command?
A2: Can I have several statements if the condition is true?

B) in another forum I found the something like this arg file:
Code: [Select]
-if
not $LensType

-LensType=Olympus OM Zuiko Auto-W 35 1:2
-FocalLength=35
-FocalLengthIn35mmFormat=50

B1: Are all the three commands executed only if the condition is true?
B2: If so: Is there no -endif or something? Or how can I end the condition?

My goal is to do 4 things, each with a different condition:
- delete ImageDescription if it is filled with "SONY DSC"
- set artist if it's empty
- set copyright if it's empty
- set some lens data it there is none (see above example).

Any help is highly appreciated!

Regards,
Karsten

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #1 on: July 07, 2011, 07:27:27 PM »
Hi Karsten,

The exiftool command will have no effect unless the -if condition is true.

You may write as many tags as you want in a single exiftool command.  The example arg file you posted writes three tag values in a single command if the LensType tag does not exist.

In this case, the order of the arguments on the command line doesn't matter.

For most of the simple tags you want to set, you don't need to use -if.  A command something like this should do most of what you want:

exiftool -imagedescription-="SONY DSC" -artist-= -artist="Me" -copyright-= -copyright="Mine" DIR

This command uses the -TAG-=VALUE syntax to conditionally delete and replace the tags you mentioned.

But to write a number of tags based on the value of a single tag you will have to use -if.  This must be done in a separate command similar to the arg file you posted.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Re: Syntax of the if-statement
« Reply #2 on: July 08, 2011, 10:21:09 AM »
Thank you so much, Phil - you made my day!

Maybe you should add a line in that description like:
- It doesn't matter where in the command the if  statement is written.
- The whole command is affected by the condition.
- When you need to do something besides the condition, you must do another command.

I hope I got it.

Your other method of a condition for just one setting is simply great.

Thanks again,
Karsten

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #3 on: July 09, 2011, 06:16:25 AM »
Hi Karsten,

Maybe you should add a line in that description like:
- It doesn't matter where in the command the if  statement is written.
- The whole command is affected by the condition.
- When you need to do something besides the condition, you must do another command.

Isn't all of this neatly summarized by?:

    "the file is processed only if the expression returns true"

The documentation is already too long for most people to read, so I try to keep it as succinct as possible.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Re: Syntax of the if-statement
« Reply #4 on: July 09, 2011, 01:51:00 PM »
Ok Phil, you are right.

Would you please help me once more? I made a little script (a batch for Win XP), but it seems like I fail right in the beginning as I always get "1 files failed condition".

@echo off
exiftool -ext jpg -ext jpeg -overwrite_original -P -if "$EXIF:FocalLength eq '0'" -EXIF:LensModel-= -EXIF:LensModel="Carl Zeiss Biogon 2,8/28 T*" -EXIF:FocalLength-= -EXIF:FocalLength="28" -EXIF:FocalLengthIn35mmFormat-= -EXIF:FocalLengthIn35mmFormat="42"  -EXIF:MaxApertureValue-= -EXIF:MaxApertureValue="2.8"  %1
pause


I made one long line because I don't like the idea of an argfile - then I would need two files for each lens.
I wanted to preserve each value if it is already filled - not sure about this idea.
For the file which I use for testing, ExifToolGUI shows me a 0 when switched to "numbers".

Thanks in advance,
Karsten

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #5 on: July 10, 2011, 07:17:11 AM »
Hi Karsten,

Use -if "$EXIF:FocalLength# eq '0'" to test the numerical value.  The "#" causes the numerical value to be returned.  Otherwise, you would need to test for something like '0.0 mm'.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Re: Syntax of the if-statement
« Reply #6 on: July 11, 2011, 08:27:45 AM »
Thanks again, Phil!

Puh, the "main" condition is working fine, but the conditions for the certain values seem to be always false. I shortened my script a bit, hoping you can see the problem at the first glance...

exiftool -if "$EXIF:FocalLength# eq '0'"  -EXIF:FocalLength-= -EXIF:FocalLength="28"   %1

(When I let the condition "-EXIF:FocalLength-=" away, it will set that value as I want.)

Thanks in advance,
Karsten

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #7 on: July 11, 2011, 08:35:57 AM »
Hi Karsten,

I don't understand what you are trying to do here.  The -if condition should be true only if the focal length is 0, however -focallength-= will cause FocalLength to only be written if it doesn't already exist.  So this command will never do anything.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Re: Syntax of the if-statement
« Reply #8 on: July 11, 2011, 09:06:38 AM »
Sorry for the dumb example, it was simply a shortening of the long script above.

So -focallength-= is not true when focallength=0?

I only wanted to avoid to overwrite existing values...

- Karsten


PS: To clarify my question: "does not exist" is not the same like =0?
« Last Edit: July 11, 2011, 09:37:16 AM by Karsten_M »

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #9 on: July 11, 2011, 09:53:52 AM »
Hi Karsten,

"Does not exist" is very different than "Exists with a value of 0".

In -if terminology, the conditions are:

"not defined $exif:focallength" - does not exist

"$exif:focallength eq '0'" - exists with a string value of '0'

"$exif:focallength == 0" - exists with a numerical value of 0

"not $exif:focallength" - either does not exist, or exists with a string value of '' or '0'

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Re: Syntax of the if-statement
« Reply #10 on: July 11, 2011, 10:02:54 AM »
Ok, thank you for the fast and extensive answer!

So ist there a analogy for:
"not $exif:focallength" - either does not exist, or exists with a string value of '' or '0'

for that short form for just for assigning one value?

Then I have another silly question: How can i print on the console the name of the file wich is processed? Just reading "1 image files updated" is not quite informative. ;-)

-karsten

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #11 on: July 11, 2011, 01:05:22 PM »
for that short form for just for assigning one value?

I don't understand.  The -if statement does not assign values.  The "not" operator is a logical operator which returns true only if its argument is a logical false.  In Perl, any variable that is not defined or has a value of '' or 0 is a logical false.

Quote
How can i print on the console the name of the file wich is processed?

When writing, use the -v0 option.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Karsten_M

  • Jr. Member
  • **
  • Posts: 17
Re: Syntax of the if-statement
« Reply #12 on: July 11, 2011, 03:10:58 PM »
Sorry, with taht short form I meant the syntax of something you explained in the beginnig here, like this:

-artist-= -artist="Me"

-Karsten

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: Syntax of the if-statement
« Reply #13 on: July 12, 2011, 08:16:22 AM »
I understand.

For conditional deletions, the following arguments delete or replace a tag under the specified conditions:

-TAG-= - tag doesn't exist or is an empty string ('')

-TAG-=0 - tag has a string value of '0'

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).