Creating keywords from modified basename

Started by tdunk, May 02, 2024, 11:53:01 PM

Previous topic - Next topic

tdunk

Hello again- I have several thousand files that i need to add keywords from part of the basename. Format is "Dr Martens_EAS-DG-504_2023-11-27_018233_01". I've successfully used listsplit for "_" but I need to remove everything after the YYYY and then get the split on "_". I've not been able to successfully find the right syntax to use. So from above example the needed keywords would be Dr Martens; EAS-DG-504; 2023.

Luuk2005

Greetings everyone! For the Windows command-line, can probably just use something like...
exiftool  -ext jpg  -overwrite_original -sep _  -keywords+"<${basename; s/^(.+?_.+?_20\d\d)-.+/$1/ or $_=undef}"  .

If worried for having too many repeated keywords, can also try something more like...
exiftool -keywords+"<${basename;s/^(.+?_.+?_20\d\d)-.+/$1/ or $_=undef}" -execute -keywords"<${keywords;NoDups(1)}" -common_args -sep _ -ext jpg -overwrite_original .
Windows8.1-64bit,  exiftool-v12.84(standalone),  sed-v4.0.7

tdunk

Thank you @Luuk2005. Well, in my noobishness, i neglected to specify we are running latest build on Mac OS so unix/linux command line.

Luuk2005

Im never used the MacOS, but I think maybe just replace the double-quotes with single-quotes?
Windows8.1-64bit,  exiftool-v12.84(standalone),  sed-v4.0.7

StarGeek

Yes, anything with a dollar sign $ in it needs to be surrounded by single quotes on Mac/Linux.  Also, the new -api NoDups option option means that you no longer need to use two commands (by way of the -execute option).

This edited version of Luuk2005's command should work, as long as there are only two keywords in the file name as per your example. If there are more, then a longer command would be needed
exiftool  -ext jpg  -overwrite_original -sep _  '-keywords+<${basename;s/^(.+?_.+?_20\d\d)-.+/$1/ or $_=undef}' -tagsfromfile @ -keywords -api nodups .
* 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).

tdunk

Thank you!! Got it all sorted out. Great community, great tool.

Phil Harvey

While I haven't read through the entire thread, I'm not sure that StarGeek's last command will do what you want.  The -tagsfromfile will overwrite existing queued values.  I think this is what you want to add the new keywords with no duplicates:

exiftool  -ext jpg  -overwrite_original -sep _  '-keywords<${basename;s/^(.+?_.+?_20\d\d)-.+/$1/ or $_=undef}' -+keywords -api nodups .

The -tagsfromfile @ isn't necessary because it is implied by the redirection argument.  The "+" before "keywords" adds the existing keywords the to list of queued keywords for writing (rather than replacing the queued list).  With this command you queue the existing keywords together with the new keywords from the file name, remove duplicates, then overwrite existing keywords with this new list.

Note that there is no "+" before the "<" because we don't want to add to the existing keywords -- this would cause duplicates.

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