News:

2023-08-10 - ExifTool version 12.65 released

Main Menu

If condition quandary

Started by mikelee33, January 28, 2015, 08:33:54 AM

Previous topic - Next topic

mikelee33

I have not used conditions before, and after reading everything I could find in the forums on this subject I am still mystified as to why this Windows batch file doesn't work:

exiftool -overwrite_original -P if "$EXIF:GPSImgDirection>247.5 and $EXIF:GPSImgDirection<292.5" -IPTC:SpecialInstructions="Looking West" -execute if "-EXIF:GPSImgDirection>292.5 and -EXIF:GPSImgDirection<337.5" -IPTC:SpecialInstructions="Looking Northwest" -execute %*

Is it simply a syntax error, or a more serious mistake?  You can see where I'm going with it -- I want to write cardinal and ordinal directions derived from the GPSImgDirection tag by dragging and dropping files onto the batch file.

Many thanks to anyone who can assist.

Mike Lee
Mike

StarGeek

I see a couple problems with it.  First, the "if" option requires a dash before it so that ExifTool recognizes that it's an option, not a file that you want to process with the name "if".

The second problem is with the -execute options.  Each time ExifTool encounters execute, it will process all statements up to that point.  If we break it down, your command is actually three commands, with only one that has a target file or directory to process.  Also, the second part of the statement has dashes in the if condition where it should be dollar signs.  As written, it would be like executing these three statements (after fixing the -if option):

ExifTool -overwrite_original -P -if "$EXIF:GPSImgDirection>247.5 and $EXIF:GPSImgDirection<292.5" -IPTC:SpecialInstructions="Looking West"

Output would be "No file specified".  This would be the only statement where overwrite_original and P options take effect.

ExifTool -if "-EXIF:GPSImgDirection>292.5 and -EXIF:GPSImgDirection<337.5" -IPTC:SpecialInstructions="Looking Northwest"

Again, "No file specified".  Plus the if condition would fail.

ExifTool %*

Full output for file or directory passed to the bat.

What you want to add is the -common_args option.  You'll end up with a command like this:

exiftool -if "$EXIF:GPSImgDirection>247.5 and $EXIF:GPSImgDirection<292.5" -IPTC:SpecialInstructions="Looking West" -execute -if "$EXIF:GPSImgDirection>292.5 and $EXIF:GPSImgDirection<337.5" -IPTC:SpecialInstructions="Looking Northwest" -common_args -overwrite_original -P  %*
* 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

#2
If you're planning on doing this for eight directions, it would be possible to do this in one command rather than eight separate if statements.  It's a bit messy looking, but try this in your batch file:

exiftool -P -overwrite_original -if "defined $GPSImgDirection" "-SpecialInstructions<${GPSImgDirection;my @Direction=('North','Northeast','East','Southeast','South','Southwest','West','Northwest','North');$_ ='Looking '.$Direction[int(($_+22.5)/45)]}" %*


Here's some sample output, changing the GPSImgDirection tag between each and then running the above command.


c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking Northeast
GPS Img Direction               : 41.26

c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking East
GPS Img Direction               : 85

c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking West
GPS Img Direction               : 255

c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking West
GPS Img Direction               : 292

c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking Northwest
GPS Img Direction               : 293

c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking Northwest
GPS Img Direction               : 292.5

c:\>exiftool -SpecialInstructions -GPSImgDirection X:\!temp\test3.jpg
Special Instructions            : Looking West
GPS Img Direction               : 292.49


Edit: added error check to make sure GPSImgDirection tag exists
* 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).

mikelee33

Sir -  I thank you very much.  My age is catching up on me ;) and I shouldn't have missed some of the "execute" logic, and certainly not the "-" in front of if. 
That you gave it extra thought and found a way to do this task in one command is going above and beyond.  I am going to try that way too, because I am curious if there will be a noticeable difference in processing time.  Again, many thanks, and have a great day.
Mike

StarGeek

Glad to have helped.  It took some time before it finally dawned on me that you would probably be trying to do it for eight directions, not just those two.  But it was a nice puzzle to figure out how to make it work.
* 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

Yes, very nice StarGeek.  Just one quick Perl tip to simplify the array initialization:

Instead of

my @Direction=('North','Northeast','East','Southeast','South','Southwest','West','Northwest','North')

you could do this:

my @Direction=qw(North Northeast East Southeast South Southwest West Northwest North)

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