deleting every keyword except the last one if it is a three characters keywor

Started by Iwonder, July 13, 2024, 05:34:58 AM

Previous topic - Next topic

Iwonder

Hello

We have the XMP-Microsoft:LastKeywordXMP tag, containing a few keywords
For SOME .jpg files of the current directory this tag contains (always at last position), a three capital letters keyword.
We'd like deleting all keywords in this tag execpt the last 3 letters keyword if it exist.
For all other files, the fields should be cleaned (no keyword in it), or deleted.
Not sure I'm clear enough then a few examples :

before :
---- XMP-microsoft ----
Last Keyword XMP                : REUNION, PHOTO-GROUPE, ADU

after :
---- XMP-microsoft ----
Last Keyword XMP                : ADU

------------
before :
---- XMP-microsoft ----
Last Keyword XMP                : REUNION, PHOTO-GROUPE
after :
---- XMP-microsoft ----
Last Keyword XMP                :

I don't know how to delete tags except the last one if it is a three characters.
A kind soul to help us please ?  :)

French newbie
Other work as volunteer : https://btaliercio.wixsite.com/easy-greek-buttons/en

StarGeek

Try this.  I'd suggest testing it out first on a few files (with backups) as I only tested it on two files.
exiftool -P -overwrite_original -sep ## -LastKeywordXMP= "-LastKeywordXMP<${LastKeywordXMP;my @temp=split '##',$_;$_=(length($temp[-1])==3?$temp[-1]:undef)}" /path/to/files/

You will get a couple of warnings for any file that is completely cleared.

The above command is for Windows CMD. If this command is run under Unix/Mac, reverse any double/single quotes to avoid bash interpretation.

C:\>exiftool -G1 -a -s -LastKeywordXMP y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[XMP-microsoft] LastKeywordXMP                  : REUNION, PHOTO-GROUPE, ADU
======== y:/!temp/Test4.jpg
[XMP-microsoft] LastKeywordXMP                  : REUNION, PHOTO-GROUPE
    2 image files read

C:\>exiftool -P -overwrite_original -sep ## -LastKeywordXMP= "-LastKeywordXMP<${LastKeywordXMP;my @temp=split '##',$_;$_=(length($temp[-1])==3?$temp[-1]:undef)}" y:\!temp\Test3.jpg y:\!temp\Test4.jpg
Warning: [minor] Advanced formatting expression returned undef for 'LastKeywordXMP' - y:/!temp/Test4.jpg
Warning: No writable tags set from y:/!temp/Test4.jpg
    2 image files updated

C:\>exiftool -G1 -a -s -LastKeywordXMP y:\!temp\Test3.jpg y:\!temp\Test4.jpg
======== y:/!temp/Test3.jpg
[XMP-microsoft] LastKeywordXMP                  : ADU
======== y:/!temp/Test4.jpg
    2 image files read
* 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).

Iwonder

Thank you @StarGeek ! it's working like a charm !
The command is so long !

I tried first with ChatGPT Guru, but it gave me 6 commands without any success, and I couldn't correct them.
Then the winner is : StarGeek ! :D
French newbie
Other work as volunteer : https://btaliercio.wixsite.com/easy-greek-buttons/en

StarGeek

Quote from: Iwonder on July 14, 2024, 01:20:55 AMThe command is so long !

Yeah, long commands like this aren't my favorite. And I prefer more sanity checks other than it's always the last entry, as there is too much of a possibility of things going wrong. But since you're sure it's always the last item, then this will work.

QuoteI tried first with ChatGPT Guru, but it gave me 6 commands without any success, and I couldn't correct them.

Thinking about this just not, I'll guess that a better solution would be to ask ChatGPT for a "Perl one-liner <what you want to do>". It does pretty good with writing short Perl code and all that would need to be done would be fit it in between the braces.

Actually, I just asked ChatGPT
"In perl, split a string into an array with "##" as the separator. If the last element of the array is three characters, return only that, otherwise return undef"

And it returned
sub split_and_check {
    my $input = shift;
    my @array = split /##/, $input;
    return (length($array[-1]) == 3) ? $array[-1] : undef;
}

Which is pretty similar, except I forgot to make it a one-liner. And, of course, I knew some keywords I wanted to use, such as "split" to get it to use the split function.
* 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).

Iwonder

@StarGeek
and of course you may easily imagine that it was NOT the question I asked to ChatGPT :D

The result is very very dependent of the way you ask. I'll try to remember this, but as a final user without any skills in developping, it's not as simple for me :)

More over I recently heard that if the user is rude with ChatGPT, the results are much less relevant ! :)

French newbie
Other work as volunteer : https://btaliercio.wixsite.com/easy-greek-buttons/en