update keywords recursively in one pass checking for current empty values

Started by jo.dor, March 15, 2020, 06:03:32 PM

Previous topic - Next topic

jo.dor

I was wanting to update keyword tags on thousands of image files with exiftool, and after some research and trial and error I figured out that when Windows 10 Explorer reads or writes keywords, it reads and writes to both "EXIF:XPKeywords" and to "XMP:Subject", the first being a string value and the second a list value.
So I'm figuring out how to recursively update in one pass the keywords for thousands of images. So far I have this:

exiftool -m -r -overwrite_original -ext jpg -ext jpeg "-XPKeywords<$XPKeywords;new keyword one;new keyword two" -Xmp:Subject+="new keyword one" -Xmp:Subject+="new keyword two" FOLDER


The only problem with this is when there currently aren't any keywords, the variable $XPKeywords comes up as empty, so the final result for the EXIF:XPKeywords field is ";new keyword one;new keyword two", but what we want is "new keyword one;new keyword two" in that case without an empty first tag.

Is there any way of handling this field differently in the same pass, according to whether it's empty or not? i.e. if it has a non empty string value, then take the current value and update it with the two new keywords, but if it has an empty value, then just create the two new keywords without trying to add them to the current value.

StarGeek

See this post for some details on what tags Windows reads and writes for image files.  You'll see that Windows also reads/writes IPTC:Keywords for it's Tags property.

Personally, I'd suggest dumping XPKeywords.  About the only program that will read it is Windows and IMO it really isn't worth the extra hassle as Windows will read XMP:Subject just fine by itself.

Otherwise, you could use this to write XPKeywords without having to worry if it already exists or not.
"-XPKeywords=new keyword one;new keyword two" "-XPKeywords<$XPKeywords;new keyword one;new keyword two"
See Note #1 under the -TAG=VALUE option for details on why this works.
* 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).

jo.dor

Quote from: StarGeek on March 15, 2020, 06:26:08 PM
Otherwise, you could use this to write XPKeywords without having to worry if it already exists or not.
"-XPKeywords=new keyword one;new keyword two" "-XPKeywords<$XPKeywords;new keyword one;new keyword two"
See Note #1 under the -TAG=VALUE option for details on why this works.
Ok after a quick test I think I see how this is working. Practically it would only work when not using either the "-m" or the "-F" options, because the first tag assignment will fail when the value is empty and so exiftool will proceed to use the second assignment (which is the first in the order written).

BTW I checked the IPTC:Keywords value of an image whose tags I updated in Windows Explorer, and it was empty. My Windows 10 Explorer doesn't seem to be using the IPTC:Keywords tag. I've tried a couple of examples and in none of my images whose tags are written by Windows 10 Explorer am I finding an IPTC:Keywords tag being written. I'm thinking maybe the post you referenced is based on an older version of Windows (either pre-10 or perhaps from an earlier version of Windows 10), seeing that the post is from 2016.

In any case, I'm not just worried about making things work for Windows, I'm hoping to make things work also for Flickr and other services, haven't tested yet but I'm guessing that most modern updated services will read the XMP tags. I think I'll simply abandon the EXIF:XPKeywords tag when doing massive updates, as you suggested.


Phil Harvey

Quote from: jo.dor on March 16, 2020, 05:30:17 AM
Practically it would only work when not using either the "-m" or the "-F" options, because the first tag assignment will fail when the value is empty

The -F option will have no effect here.  Only the -m option would change this.

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

Stephen Marsh

Just to confirm what Windows 10 is producing for me for properties "tag" (keyword/subjec)t:



[IFD0] XPKeywords

[XMP-dc] Subject

[XMP-microsoft] LastKeywordXMP


jo.dor

I might also add that, in my testing, if I manually create IPTC:Keywords, Windows 10 Explorer will then update it, but if it doesn't exist it won't create it.

StarGeek

Quote from: Stephen Marsh on March 21, 2020, 07:18:58 AM
[XMP-microsoft] LastKeywordXMP

Interesting, that's new.

When adding a keyword on the Properties->Details tab, Windows is setting XMP:LastKeywordXMP and, if an IPTC block already exists, XMP:LastKeywordIPTC.  It does not, however read these tags.
* 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).