Copy filename to list of keywords

Started by SlavePunisher, July 27, 2017, 08:07:40 AM

Previous topic - Next topic

SlavePunisher

Hi there,

Since a very long time, I used to keep organized my pictures by renaming them with date and time, original name, and a list of keywords.

Now I would like to use OS X Photos with iCloud. I'm trying to write a command to copy the list of keywords from the filename, to the keyword tag, to be able to retrieve and use the keywords in the Photos app.

I tried this one:
exiftool -overwrite_original '-keywords<${filename;s/\.jpg$//i}' -P

I 'works', but it only writes one keyword from the whole filename. I then tried with -sep' ' (space as a keyword separator), but it returns a
QuoteIgnored superfluous tag name or invalid option
error.

Is there any way to make separates keywords from a filename like "2004 04 07 - 11h38m36s - DSC_0050 - Voyage France beach sea people" ?

Thanks a lot for your help !

Phil Harvey

You need a space after -sep:

exiftool -sep ' ' ...

However, with your example file name you will get the following keywords, which isn't ideal:

2004
04
07
-
11h38m36s
-
DSC_0050
-
Voyage
France
beach
sea
people

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

SlavePunisher

Wow !! Many many thanks Phil. I've been working around this command for a while, so it was just a syntax error  ;D ::)

Effectively, it works, but it is not ideal. Would there be a way to avoid certain kind of characters ? Or maybe avoid the 36 first characters ?

Phil Harvey

To avoid the first 36 characters (and also remove the last 4 characters of the extension) :

'-keywords<${filename;$_=substr($_,36,-4)}'

But if you wanted to keep the date/time and old filename, you could do this:

'-keywords<${filename;s/\.jpg$//i;s/(\d{4}) (\d{2}) (\d{2})/$1-$2-$3/;s/ - / /g}'


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

Stephen Marsh

#4
Another method is to use a regex to only capture the words at the end, but not the filename extension. This may or may not always be after the first 36 characters, so it is probably safer to use a variable pattern rather than a fixed number of characters (presuming that the pattern is consistent of course):

exiftool -sep ' ' -use MWG '-keywords<${filename;s/(^.+-\s)(.+)(\..+$)/$2/}' '2004 04 07 - 11h38m36s - DSC_0050 - Voyage France beach sea people.jpg'

Which would only add the following as keywords:

Voyage;France;beach;sea;people

P.S. You should be safe to use -subject rather than -keywords however you could also play it safe by using -use MWG with -keywords to write to both subject and keywords tags.