Move exisitng folder name (structure) into image "tag"

Started by jappie, September 12, 2013, 07:33:43 AM

Previous topic - Next topic

jappie

Hello,

I have a folder structure for my images whereby the folder name (and structure) is an indication of the tags I would like to retrieve the images by in a new structure that is %Y/%m/%d based. I have no problems to create this new structure but my question is if it is possible to somehow "move" the names of the exisiting folders into the image "tags"

an example:
image located in family/holiday/corsica_2007 --> will go to 2007/07/15/2007-07-05_10:25:34.jpg. I would like this image to have the tags "family", "holiday" and "corsica_2007" associated with the file.

Any ideas, tips or tricks?

Thanks a lot for your help.

Cheers JP

Phil Harvey

Hi JP,

Sure.  See this post for someone else who did the same thing.  (The forum search feature is your friend.)

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

jappie

Hi Phil.

Have to say; you are fast! Thanks for pointing me to the right thread I thought I'd seen them all, but alas.
I've got it working with the config file and would like to "add" to "subject" instead of "keywords" so changed:

  • "-keywords<dirtree" to
  • "-subject<dirtree"

This works fine but it overwrites any existing tags that may already exist.
Questions:
- is there any way of "adding to" instead of "overwriting" for this option?
- how can I eliminate the leading folder (.) that is included in this tag as the first keyword?

Any help much appreciated, cheers JP

Phil Harvey

Quote from: jappie on September 12, 2013, 10:08:20 AM
- is there any way of "adding to" instead of "overwriting" for this option?

Yes:  "-subject+<dirtree"

Quote- how can I eliminate the leading folder (.) that is included in this tag as the first keyword?

Type "*" instead of "." on the command line.  Either that, or the conversion could be modified to add this line:

shift @parts if @parts and $parts[0] eq '.';

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

jappie

Hi Phil,

This is growing on me and I'm getting closer yet also start to feel (a bit) lost...
Now when I run the command the terminal feedback in any cases is: "Warning: Shift value for XMP-pdf: Subject is not a number - <filename>"

For these files the subject is then not added and the tag is instead added to "Keywords".

Logical for some maybe but I might be missing something here.

BTW: the command I run is the following:
exiftool -config config_tags "-subject+<dirtree" * -r -P -d '%%e/%Y/%m/%d-%m-%Y/%d-%m-%Y_%H.%M.%S' '-filename<${CreateDate;}-(%f)%-c.%e' '-filename<${DateTimeOriginal}_${Make;}_${Model;}-(%f)%-c.%e'

whereby " config_tags" contains the instructions as per the link you directed me to...
As a final observation I note that some files with for instance .png or .tiff extensions are not following the "%e" switch and do not contain CreateDate, thus throwing other errors. This is not a major problem but I'd like to set up a fully comprehensive script for my needs.

Any insights here?

Cheers JP

Phil Harvey

Quote from: jappie on September 12, 2013, 11:30:07 AM
Now when I run the command the terminal feedback in any cases is: "Warning: Shift value for XMP-pdf: Subject is not a number - <filename>"

Specify xmp-dc:subject instead of just subject to avoid warnings when trying to set other tags.

QuoteFor these files the subject is then not added and the tag is instead added to "Keywords".

No.  You're fooling yourself somehow here.  XMP-dc:Subject will still be written properly, and Keywords will not be written unless you specify "Keywords" in the command.

QuoteAs a final observation I note that some files with for instance .png or .tiff extensions are not following the "%e" switch and do not contain CreateDate, thus throwing other errors. This is not a major problem but I'd like to set up a fully comprehensive script for my needs.

You need to decide how to name files without a CreateDate or DateTimeOriginal.  Then just assign a default '-filename=some_default-(%f)%-c.%e' before attempting to copy the filename from CreateDate and DateTimeOriginal.

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

jappie

Hi Phil,

it's proving to be more difficult than I anticipated but am very willing to learn.
My preferred default file name structure would be "%d-%m-%Y_%H.%M.%S%-c.%e"

Then how do I pass this default to the line as a "default"? Right now I get results like this: "-%m-%Y_%H.%M.%S-9" and on top, the desired hierarchical structure (/YY/MM/DD-MM-YYYY) is not created.

Quoteexiftool -config config_tags "-xmp-dc:subject+<dirtree" * -r -P -d '%%e/%Y/%m/%d-%m-%Y/' '-filename=%d-%m-%Y_%H.%M.%S%-c.%e' '-filename<${CreateDate;}%-c.%e' '-filename<${DateTimeOriginal}_${Make;}_${Model;}%-c.%e' -ext jpg

Thank you very much for your help already this far, I'm hoping you can give me the final push in the right direction.

Cheers JP

Phil Harvey

I'm confused.  How do you propose to fill in the year, etc for your default file name if there is no CreateDate or DateTimeOriginal?

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

jappie

It must be me that is confused and is thus creating confusion;

in the instructions (or an example I found somewhere) I found a reference to the ';' as a means of creating different naming "schemes".
I am under the impression that the code I posted previously uses a sort of "hierarchy" of proposed naming schemes, like :

1. '-filename=%d-%m-%Y_%H.%M.%S%-c.%e' is the default, yet if there is a CreateDate then use
2. '-filename<${CreateDate;}%-c.%e' and if there is also a make and model to be retrieved use,
3. '-filename<${DateTimeOriginal}_${Make;}_${Model;}%-c.%e'

So to answer your question (and this is where your coaching style brings the boomerang back), I now see how the 1st statement is also dependent on the presence of either a CreateDate or DateTimeOriginal... So, if I understand correctly the first statement should make no reference to EXIF data as they might not be present. Good.

Will that also solve my problem with the directory structure no longer being respected?

Cheers JP

Phil Harvey

I'm glad things are starting to make a bit of sense now.

Quote from: jappie on September 12, 2013, 05:36:02 PM
Will that also solve my problem with the directory structure no longer being respected?

yes, provided your default file name has the structure you want.

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

jappie

Hi Phil,

I turns out that one of my problems was that I wanted to use different -d formats for the directory structure and the file name. I found another thread where you explain and advice to do this in two steps. Works a dream now so that's great. Thank you for your great tool and fabulous support!

One last comment; I noticed that the images taken with my Nexus smartphone throws warning "Invalid EXIF text encoding" yet the file gets renamed according to the desired output. Thought you might want to know.

For other readers of this forum here is the list of commands I have used

1. To create a new folder structure (/extension/Model/YYYY/MM/DD-MM-YYYY) whilst moving the existing folder structure names into tags  I used:
#Pass 1a: move files with EXIF into the proper directory:
exiftool -config config_tags '-xmp-dc:subject+<dirtree' '-Directory<%e/${MyModel}/$DateTimeOriginal' -d '%Y/%m/%d-%m-%Y' -r *

Note: this is dependent on the configuration script attached to this post.

1b. move files with no EXIF:
#Pass 1b: move files with no EXIF:
exiftool -r '-directory</Imagearchive/NOEXIF' *


2.  rename the files (with EXIF) to include references to Make and Model
#Pass 2: rename the files (with EXIF):
exiftool -config config_tags -r -d '%d-%m-%Y_%H.%M.%S' '-filename<photo;_(%f)%-c.%e' '-filename<${CreateDate;}%-c.%e' '-filename<${DateTimeOriginal}_${Make;}_${Model;}-(${MyFilenumber})%-c.%e' *


3. (Optional) | Reset the file date and time to the date taken

#Pass 3: reset file dates to date created
exiftool "filemodifydate<datetimeoriginal" *


Open a beer and enjoy the fruits of your work!


Phil Harvey

Excellent!  I'm glad you got things working the way you wanted.

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

effgee

Hi there jappie and Phil,

jappie, your set of commands does almost everything I was looking for, thank you!

I am having a weird problem that I just can't figure it out.

On Pass 1a, it says, move files with EXIF into the proper directory:

On my system (Ubuntu Linux), it doesn't move them, it copies them. Can someone explain why? I would like them to move.

Phil Harvey

Can you give the exact command you are using that copies the files?  Also, are you moving them to a different filesystem, or just another directory within the same filesystem?

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

brightwolf

I am trying to copy this trick to automatically set Keywords in my photo files based on folder structure.

My structure looks like this:
Album
-- Holiday
-- -- 2010 Paris France
-- -- 2011 Montpellier France

The photos are only in the last folders of the tree. I would like the photos in "2010 Paris France" folder to get these keywords: Album, Holiday, 2010, Paris, France

So basically split the folder tree based on '/' as in config_tags, but also split the folder name based on ' '.

How does this work? All help appreciated.
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

Phil Harvey

This is one way:

exiftool "-keywords<${directory;tr( )(/)}" -sep "/" -r DIR

This command translates all spaces to "/" in the directory name, then splits them into separate keywords at the /'s.

And here is another way:

exiftool "-keywords<directory" -api listsplit="[/ ]" -r DIR

which uses the API ListSplit option to split on spaces or slashes.  (The API ListSplit option allows you to specify a regular expression, while the command-line -sep option does not.)

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

StarGeek

I believe the original post was before the advanced formatting feature was added.  With an up to date copy of exiftool, you could do this inline.

Assuming "Album" doesn't occur anywhere else in your directory structure, you could do this:
exiftool -r -sep "," "-keywords<${directory;s/.*\/(Album.*)/$1/;s/( +|\/)/,/g}" FileOrDir

This will first remove the directory path that's before "Album" (note that this is case sensitive), then replace all spaces and slashes in the paths with commas.  The sep option then sets the keywords using the comma as a separator.
* 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

@StarGeek:  Good point about removing stuff before Album.  I was implicitly assuming that DIR would be "Album", which would require cd-ing first to the parent directory.  But other than that we had a similar initial idea.  But my 2nd thought of using the ListSep option is perhaps a bit simpler, but does require cd-ing to the parent directory first.

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

StarGeek

:D

Even though I know you posted an answer before me, I'm not going to let my slowness stop me from saying "I had an idea, too!"  I already invested the time in typing up an answer and double checking it, so I'm going get my say in.

* 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

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

brightwolf

Excellent, Phil & StarGeek. Thanks for your quick answers. I already started changing the config file to use regex in the split (and running into problems with that, since all / and space characters were created as keywords too). The command line with -listsplit is so much simpler and yields exactly the desired result!

Note that "exiftool -r -sep "," "-keywords<${directory;s/.*\/(Album.*)/$1/;s/( +|\/)/,/g}" FileOrDir' yields a "bad substitution" problem. Since the "listsplit" option works for me, I did not further look into that.. 
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

StarGeek

Hmmm...  What version of exiftool are you using?  Or are you on Linux/Mac? 

The command should work.  I just copy/pasted it right now to test it and no error.

Either way, no worries, as long as you got something working for you.
* 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).

brightwolf

#22
Hi StarGeek. No worries...
I am using exiftool 10.21 on a Mac.

One more question I have about this command. It preserves existing keywords (which is good) but it also preserves the original photo as <photofile>_original. Why is that? Does updating the EXIF tags reprocess the JPG, causing some information loss? Or is it for another reason? If no quality is to be expected, can I somehow prevent the copy from happening? Note that I have more than 55000 photos to process with this command, amounting to almost 500 GB of data, and I have no intention of doubling that. Note also that I already have a backup (multiple, in fact, both on and off site). 
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

StarGeek

Quote from: brightwolf on March 29, 2017, 03:42:48 PM
I am using exiftool 10.21 on a Mac.

Ah, the problem then was with the double quotes.  Any time Mac/Linux has a $ in the command, then single quotes need to be used instead of double quotes (see .sig on Phil's post).

QuoteIt adds to the Keywords tag (which is good)

One thing that we forgot to mention is that these commands will overwrite the keywords with the new data.  If there already were some keywords, these will be lost.  If any of the files already have keywords, use +< instead of <.

Quotebut is also preserves the original photo as <photofile>_original. Why is that?

Exiftool will normally make a backup of the original file.  See second paragraph under Description in the docs.  If you want to avoid that, add -overwrite_original to the command.  Or you can delete them afterwards with the -delete_original command.

QuoteDoes updating the EXIF tags reprocess the JPG, causing some information loss?

Exiftool only changes metadata, never the original image.  Though there are a few metadata tags that can affect how the image is show, ICC_Profile for example.  But then, those are tags you shouldn't be messing with unless you know what you are doing anyway.
* 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).

brightwolf

OK clear, thanks for the quick update. I already replaced 'keywords<' with 'keywords+<', this indeed has the desired effect of existing keywords being preserved. As far as the copy is concerned, I will add the flag you mentioned. Thanks!
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

brightwolf

I have one observation I want to share here.

StarGeek, in your command using -sep, you included some regex to replace all folders before the 'Album' one so those would not show as keywords.

However, when either CD'ing to the Album folder and then running the exiftool command, or when mentioning the full folder in the exiftool command itself (eg User/Album/* instead of *) the exiftool ignores that first part of the folder structure already, and there's no need to replace it in the regex.

Is this intended behaviour? It works for me, though. Just wanted to share this observation.
Photographer. Hobbyist. Using iMac to manage photos, with Photo Mechanic Plus, Pixelmator, Luminar, and exiftool. Longing back to Aperture sometimes.

StarGeek

I'm not sure I understand your question.  If you CD to the directory, the regex doesn't match and no replacement is done and all is fine.  But if you use the full path in the command, then directories above the "Album" should also be included.  For example, "X:" and "!temp" are included if my path is X:\!temp\Album and I don't include the first regex
C:\Programs>exiftool -r -sep "," "-keywords<${directory;s/( +|\/)/,/g}" X:\!temp\Album
    4 directories scanned
    2 image files updated

C:\Programs>exiftool -r -keywords X:\!temp\Album
======== X:/!temp/Album/Holiday/2010 Paris France/20151212-12.12.12.jpg
Keywords                        : X:, !temp, Album, Holiday, 2010, Paris, France
======== X:/!temp/Album/Holiday/2011 Montpellier France/20151212-12.12.12.jpg
Keywords                        : X:, !temp, Album, Holiday, 2011, Montpellier, France
    4 directories scanned
    2 image files read


I can't test the command on mac since I don't have access, but if you don't cd to the directory and instead use the full path, in your example, "user" should also be added to the keywords.
* 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).