News:

2023-03-15 Major improvements to the new Geolocation feature

Main Menu

Getting part of folder name into EXIF description

Started by slashedzero, January 06, 2017, 07:25:55 AM

Previous topic - Next topic

slashedzero

Hi there! New to exiftool and the impressive array of functionalities it offers. What I am trying to do is this:

Many of my folder names have a description for the photos they contain. There is, however, not a completely rigid format for the folder name. Three types exist:
(1) 2014\2014_07_21 - Birthday
(2) 2014\2014_08_01-15 - Holiday in Spain
(3) 2014\2014_09_15

Hence, some have just one date with a description, others a range of dates with a description, and some don't have a description at all.

What I would like to do is, recursively on my whole Photo folder (of which the above would be direct subfolders):
- get the start of the description from the folder name, if there is any (I would think by locating " - " with an index function?)
- put the description in a string
- write that string to the EXIF Description of all image files in the folder

I had found some posts closely related to this, but the extra difficulty of first getting the substring with the " - " delimiter proves a challenge...

Anyone care to be so kind to hep me out? Thanks!!

(PS: Pictures are stored on Synology NAS; I could run exiftool there, or go via my Windows PC.)

Phil Harvey

Try this:

exiftool -r "-imagedescription<${directory;s/.* - // ? $_ : undef}" DIR

Here I assume you meant EXIF:ImageDescription.  There is no EXIF tag named "Description".

This command avoids writing a description if none existed in the directory name by checking the returned value of the substitution and setting the value to "undef" if no substitution was made.

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

slashedzero

Many thanks, Phil! Doing some testing, but seems to work fine, apart from writing the folder name to the ImageDescription (indeed, the tag I intended) in case there is no description given in the folder name. So the 'undef' trick may not be working (but still investigating); I'm doing the tests in a Windows environment BTW.

slashedzero

Update: The ternary operator (? : ) is basically left untreated, it turns out, the processed (substituted) string is always returned and written. Hence, if there's no substitution to be made (no description), the folder name is written to the tag. Any idea how to solve this?

Phil Harvey

Right, I forgot to assign "undef" back to $_.  The command should have been:

exiftool -r "-imagedescription<${directory;$_ = s/.* - // ? $_ : undef}" DIR

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

slashedzero

Great - that does the trick! I do understand the gist of what's going on, but have too little knowledge of exactly how values are returned etc in Perl / regex to be able to come up with that. Many thanks!! (And congrats for the amazing work on exiftool!)

devoid

Hi there, and thanks for this great tool.
I'm dealing with the same task as slashedzero, but my folders are all like:
"2019-01-06 Trip to Mars"

So no " - ", just the date followed by space.

I'm trying to modifiy the code, but no luck.

Any help will be greatly apreciated.

Thanks!

Phil Harvey

Try this:

exiftool -r "-imagedescription<${directory;$_ = s/.*? // ? $_ : undef}" DIR

The ? causes .* to match the fewest number of characters possible (before the space).  Without this, you would only get the last word in the directory name.

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

devoid

Thanks for the quick reply.
It works!! Thank you.

Although the Parent folder for the subfolders can't have any spaces in it.
Ex:
"\Main folder\2019-01-06 Trip To Mars"
Results in:
"folder\2019-01-06 Trip To Mars"

Also in leaves the space in ImageDescription:
" Trip To Mars"

Thanks again!

Phil Harvey

Right.  You could remove the parent folder first, then everything up to and including the space:

exiftool -r "-imagedescription<${directory;s(.*/)();$_ = s/.*? +// ? $_ : undef}" DIR

I didn't understand your comment about the extra space, so I added a "+" to avoid this problem if there were two spaces before the description.

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

user10101

Is there a way to set the description equal to the full parent folder name?

On a Mac, I tried this:

exiftool -r '-imagedescription<${directory;s(.*/)();$_ = s/.*? +// ? $_ : undef}' IMG00001.jpg

and got back this:

Warning: [minor] Advanced formatting expression returned undef for 'directory' - IMG00001.jpg
Warning: No writable tags set from IMG00001.jpg
    0 image files updated
    1 image files unchanged
Adams-iMac:Pinetop 1960 Adam$

StarGeek

The reason nothing was updated is because you're in the same directory as the image.  If you run
exiftool -directory IMG00001.jpg
you'll see that the results are
Directory                       : .

A single dot for the directory stands for the current directory and the as you can see, doesn't show the parent directory.  You can either replace  IMG00001.jpg with the full path to the file or directory, or use a more complicated expression that makes use of the FilePath variable.

Try this:
exiftool '-imagedescription<${FilePath;$_=(split '\/',$_)[-2]}' IMG00001.jpg

For more details on how this comand works, see this post.
* 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).

Japester

My needs are much simpler. I have a set of folders where I want to write the whole name of the folder into the ImageDescription tag. I don't need to extract part of the folder name first. How would I write a command to do this?

Phil Harvey

1. "cd" to the folder containing all of the folders with the images.

2. exiftool "-description<directory" -r *

This will put the relative directory path into the Description.  If you want an absolute path instead, do this:

1. exiftool "-description<directory" -r c:\path\to\root\folder

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

Japester

Thanks for this. I'm struggling with this. I'm on macOS. I open Terminal and cd to the folder, which in my case is:

/Volumes/Main\ Storage/\ Lightroom\ Import/Africa\ puzzle

I have one file in this folder. I enter this command:

exiftool "-imagedescription<directory" -r *

I observe the result with this command:

exiftool -ImageDescription *

The ImageDescription is now a period, ., not the name of the folder. What have I done wrong? Thanks.