-if condition trouble

Started by IXix, January 28, 2018, 03:47:07 PM

Previous topic - Next topic

IXix

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!

StarGeek

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.  You can use a site like RegEx101 to test out your regex.
"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

IXix

Thanks! I think I'll be able to get it working now. :)

Stephen Marsh

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?

Phil Harvey

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

Stephen Marsh

Thank you Phil, of course, I only tested with 1 keyword! Should have known better than to only do the minimum...

StarGeek

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.
"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

IXix

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! :)

StarGeek

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.
"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

IXix

Good point, I hadn't thought of that!

Stephen Marsh

#10
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

IXix

Thanks, I'll look into that next time.  :)