Use parts of the folder names as keywords in JPG-Files

Started by ThePhantom79, April 20, 2024, 04:28:28 AM

Previous topic - Next topic

ThePhantom79

Hello,

I dont know, which way I should choose to update my library with keywords. Maybe it's to complicated - I don't know.

I am using for my vacations folder this structure:

C:\Users\Chris\Pictures\Urlaube\201311 Athen
C:\Users\Chris\Pictures\Urlaube\201311 Athen\_Auswahl
C:\Users\Chris\Pictures\Urlaube\201307 Thailand
C:\Users\Chris\Pictures\Urlaube\201307 Thailand\_Auswahl



My goal is it, to to add the the word / words after the 6digits as a keyword into my jpgs. On some folders, there are subfolder, that has names like "_Auswahl", in this subfolder, the jpgs should additionally have the keyword "Auswahl".

I could do it manually, folder by folder, with these commands:

./exiftool -r -EXT JPG -overwrite_original -keywords+=Athen "C:\Users\Chris\Pictures\Urlaube\201311 Athen"
./exiftool -r -EXT JPG -overwrite_original -keywords+=Auswahl "C:\Users\Chris\Pictures\Urlaube\201311 Athen\_Auswahl"

Is it possible to automate this?

And - is it possible to run this once per day  for all folders, and add these tags only, if the keywords are not  already set? (This could run on as a task on my PC or on synology NAS - then new pictures could be tagged automaticly. Already tagged pictures would be not touched).
IMHO this could be better done by a bash script on Linux and not only by exif tool, or what do you think?


Phil Harvey

#1
First, I would recommend using XMP:Subject instead of IPTC:Keywords.

You can do what you ask like this:

exiftool "-+subject<${directory;s/.*\d{6}//;$_=join '##', /[a-z]+/ig}" "-+subject" -api nodups -sep ## DIR

I know the command is a bit complex, but I don't have time to explain all this right now.

- Phil

Edit: Fixed command to use Directory instead of Comment (thanks StarGeek)
...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 ($).

StarGeek

Quote from: Phil Harvey on April 20, 2024, 07:29:16 AMexiftool "-+subject<${comment;s/.*\d{6}//;$_=join '##', /[a-z]+/ig}" "-+subject" -api nodups -sep ## DIR

I think you mean Directory, not Comment.

An additional caveat is that you cannot CD into a directory that contains a keyword you want to use. You have to either be above it or use an absolute path. In your example, you could CD into
C:\Users\Chris\Pictures\Urlaube
but not
C:\Users\Chris\Pictures\Urlaube\201311 Athen
* 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).

Phil Harvey

Quote from: StarGeek on April 20, 2024, 12:00:05 PMI think you mean Directory, not Comment.

Right, thanks.  I was testing the command with the directory name in the comment field.  Fixed.

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

ThePhantom79

Thansk a lot for explanation - I will have a detailed look later on it. Before I use it, I have to understand it ...

Phil Harvey

OK.  A bit of explanation:

"-+subject<${directory;s/.*\d{6}//;$_=join '##', /[a-z]+/ig}"

- uses the implied -tagsFromFile top copy the directory name and add it to the queued items to write to XMP Subject.  s/.*\d{6}// removes everything in the directory name up to and including the first 6-digit string.  /[a-z]+/ig returns a list of all words in the remaining string, and $_=join '##', joins these words with '##' as a separator and assigns this as the return value in $_.

"-+subject"

- add back the items already existing in the Subject tag

-api nodups

- removes duplicate items from queued lists

-sep ##

- separates strings written to list-type tags (eg. Subject) at "##" strings

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

ThePhantom79

Thanks again ...
So, the first 6 charackters will be always removed, correct? That means for the subfolder, that do not start with these digits, I will change the command?  I can't use this command recursivly to all subfolders - correct? I have to run it for each folder seperatly. I could do this with a bash script ...

StarGeek

Quote from: ThePhantom79 on April 22, 2024, 04:59:12 AMSo, the first 6 charackters will be always removed, correct?

Only if they are numbers. In Regular Expressions (RegEx), \d means any single digit character. The {6} means there be exactly 6 of whatever character is to the left of this. The result being that it will match 6 digits.

QuoteThat means for the subfolder, that do not start with these digits, I will change the command?

Yes.  It would require a different RegEx.

QuoteI can't use this command recursivly to all subfolders - correct? I have to run it for each folder seperatly.

It will be recursive if you add the -r (-recurse) option, adding any directory name after the first one with six numbers.  Also, there would have to be a minor change if the directory names included any character other than A-Z, such as a space, apostrophe, accent characters, etc.
* 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).