ExifTool Forum

ExifTool => Newbies => Topic started by: IXix on January 28, 2018, 03:47:07 PM

Title: -if condition trouble
Post by: IXix on January 28, 2018, 03:47:07 PM
Hi,

I want to replace the keyword "Me" with "Phil" in any files that have it.  I tried this...

exiftool -keywords-="Me" -keywords+="Phil" -if "$keywords =~ /Me/" "C:\Photo\"

...which almost works but the condition doesn't treat "Me" as a whole word, so it matches "Megan" and "Melvin" etc.

I haven't been able to find documentation for the match expression syntax. What do I need to do to get the desired behaviour?

Thanks in advance!
Title: Re: -if condition trouble
Post by: StarGeek on January 28, 2018, 04:09:15 PM
Trying to exactly match a single keyword with an IF can get a bit messy.  I'd suggest using the -api Filter option and -TagsFromFile, as that will process each keyword individually, rather than the full list as in your IF condition.

Try
exiftool -if "$Keywords# ne $Keywords" -api "Filter=s/^Me$/Phil/" -TagsFromFile @ -Keywords C:\Photo\

The match expression syntax is called Regular Expressions (RegEx) and it's a whole programming subject by itself.  There are lots of tutorials on the web.  The site I used to learn from is regular-expressions.info (https://www.regular-expressions.info/).  You can use a site like RegEx101 (http://regex101.com) to test out your regex.
Title: Re: -if condition trouble
Post by: IXix on January 29, 2018, 03:39:49 PM
Thanks! I think I'll be able to get it working now. :)
Title: Re: -if condition trouble
Post by: Stephen Marsh on January 30, 2018, 06:55:13 AM
I would have offered the following (swap the single/double quotes around for Windows):

exiftool -keywords='Phil' -if '$keywords eq "Me"' FILEorFOLDER

Can you please explain why this would be an messy or a possible issue StarGeek?
Title: Re: -if condition trouble
Post by: Phil Harvey on January 30, 2018, 07:06:37 AM
If there is more that one keyword, then $keywords expands to something like "Me, You", and your -if condition will fail.  To handle multiple keywords in a -if condition, the command would have to be something like this:

exiftool -keywords-="Me" -keywords+="Phil" -if "$keywords =~ /(^|, )Me(, |$)/" "C:\Photo\"

Which I think will work in all cases unless there is a ", " within a keyword.

- Phil
Title: Re: -if condition trouble
Post by: Stephen Marsh on January 30, 2018, 07:17:26 AM
Thank you Phil, of course, I only tested with 1 keyword! Should have known better than to only do the minimum...
Title: Re: -if condition trouble
Post by: StarGeek on January 30, 2018, 11:00:45 AM
Quote from: Phil Harvey on January 30, 2018, 07:06:37 AM
exiftool -keywords-="Me" -keywords+="Phil" -if "$keywords =~ /(^|, )Me(, |$)/" "C:\Photo\"

Which I think will work in all cases unless there is a ", " within a keyword.

This is basically what I use except I add the -sep option because I do have commas in my keywords from time to time.  Of course, it's just my opinion that it's messy and I really like the filter option.
Title: Re: -if condition trouble
Post by: IXix on January 30, 2018, 05:13:04 PM
FWIW after a bit (a lot) of trial and error, I ended up using the following...

exiftool -keywords-="Find" -keywords-="Replace" -keywords+="Replace" -r -P -if "$keywords =~ /\bFind\b/" "C:\Photo"

...which worked a treat on the IPTC keywords and then this...

exiftool -subject-="Find" -subject-="Replace" -subject+="Replace" -r -P -if "$subject =~ /\bFind\b/" "C:\Photo"

...to handle the XMP as well.

There's probably a way do both in a single command with a logical OR but there was too much scope for error so I decided to play it safe.

Thanks again for the help! :)
Title: Re: -if condition trouble
Post by: StarGeek on January 30, 2018, 05:27:03 PM
Quote from: IXix on January 30, 2018, 05:13:04 PM
"$keywords =~ /\bFind\b/"

Just one thing to watch for with this.  This regex will match "Find" in cases where Find is just one word in a multi-word Keyword.  For example
C:\>exiftool -g1 -s y:\!temp\Test3.jpg -keywords
---- IPTC ----
Keywords                        : Find And Replace

C:\>exiftool -P -overwrite_original -keywords-="Find" -keywords-="Replace" -keywords+="Replace" -r -P -if "$keywords =~ /\bFind\b/" y:\!temp\Test3.jpg
    1 image files updated

C:\>exiftool -g1 -s y:\!temp\Test3.jpg -keywords
---- IPTC ----
Keywords                        : Find And Replace, Replace


Of course, if you know your own data and know this won't happen, then don't worry about it.
Title: Re: -if condition trouble
Post by: IXix on January 31, 2018, 05:45:10 AM
Good point, I hadn't thought of that!
Title: Re: -if condition trouble
Post by: Stephen Marsh on January 31, 2018, 08:01:37 AM
Quote from: IXix on January 30, 2018, 05:13:04 PM
There's probably a way do both in a single command with a logical OR but there was too much scope for error so I decided to play it safe.

Using the following Metadata Working Group tag should write to both IPTC keywords and XMP subject:

-use MWG -keywords=

or


-MWG:Keywords=


https://exiftool.org/TagNames/MWG.html
Title: Re: -if condition trouble
Post by: IXix on January 31, 2018, 03:31:12 PM
Thanks, I'll look into that next time.  :)