Conditionally add a leading string to subject and keyword tags

Started by brunos, November 03, 2021, 07:09:51 AM

Previous topic - Next topic

brunos

Hi all!

I would need some help, if possibile, as my attempts to do it on my own, failed.

SITUATION:
My photos have a zillion of tags in -subject and in -keywords (the ones in -keywords are the precise duplicates of the ones in subject)
I need to add a prefix "t:" to all those tags.
The existing tags can be a list, and some may have a leading ":" colon punctuation mark, while others may not have it. In this example the first tag has a leading colon, while the second hasn't it:

Subject                         : :test:1:2, test:3:4
Keywords                        : :test:1:2, test:3:4


The expected result should be (also after many re-runs of the updater):

Subject                         : t:test:1:2, t:test:3:4
Keywords                        : t:test:1:2, t:test:3:4


MY ATTEMPTS
I have tried a simple thing, firstly only on subject and on a current folder containing a photo that had two tags in subject and keywords:
exiftool "-xmp-dc:subject<t:$xmp-dc:subject" .

The result was:
Subject                         : t::test:1:2, test:1:2

It worked, but two things were wrong: the addition to the first tag resulted in doubled colon (as it had a leading colon), while the second tag wasn't updated at all. The first wrong thing is understandable, as I didn't provide for any code that would prevent adding a "t:" to an existing leading ":"; why it didn't update the second tag, I don't know.

I've searched through forum and through my collection of examples how to handle "add "t:" if the first two chars are not "t:" else if the first char is a ":", add "t"), but I didn't find anything I could comprehend and apply.

Kindest regards and thanks in advance
Bruno

Phil Harvey

Hi Bruno,

You should be aware of FAQ 17 when performing list operations like this.

You can do what you want with advanced formatting expression.  The command could look like this:

exiftool "-subject<${subject@;s/^:?/x:/}" "-keywords<${keywords@;s/^:?/x:/}" -sep "||" DIR

The expression removes a leading colon, then adds "x:".  The "@" symbol makes the expression act on each item in the list.  The -sep option is necessary to split the string back into individual list items when writing.

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

brunos

Thanks Phil for the prompt help!

I've tried, and in the first run it does it ok. But in the second run, it duplicates my t's:

Subject                         : t:t:test:1:2, t:t:test:3:4:
Keywords                        : t:t:test:1:2, t:t:test:3:4:


Since I might need to run it more times, I would need that it doesn't act if there's a leading t: in the content (or a leading x: as in your example).

Thanks again!
Bruno

Phil Harvey

Hi Bruno,

Here you go:

exiftool "-subject<${subject@;/^x:/ or s/^:?/x:/}" "-keywords<${keywords@;/^x:/ or s/^:?/x:/}" -sep "||" DIR

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

brunos

Thanks a zillion, it works perfectly.

Just wondering is there around a tutorial you can recommend with many examples for dummies such as myself, to try to understand the logic behind the syntax and the variations.

I'm programming in VB.Net, VBA and Autoit and would have no problems to solve similar problems in those languages (surely not in a high elegance, still functional enough), but the Perl's syntax and alikes are driving me crazy... :(

Thanks again!!!

Kindest regards,
Bruno


Phil Harvey

Regular expressions look cryptic, but they are easy to use once you get the hang of it.  The s/// syntax is a Perl substitution which uses regular expressions.  You can google for lots of examples and tutorials on regular expressions.

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

brunos