# ExifTool Forum

## ExifTool => Newbies => Topic started by: slashedzero on January 06, 2017, 07:25:55 AM

Title: Getting part of folder name into EXIF description
Post by: slashedzero on January 06, 2017, 07:25:55 AM
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.)
Title: Re: Getting part of folder name into EXIF description
Post by: Phil Harvey on January 06, 2017, 08:32:41 AM
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
Title: Re: Getting part of folder name into EXIF description
Post by: slashedzero on January 08, 2017, 04:29:06 AM
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.
Title: Re: Getting part of folder name into EXIF description
Post by: slashedzero on January 08, 2017, 05:23:02 AM
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?
Title: Re: Getting part of folder name into EXIF description
Post by: Phil Harvey on January 08, 2017, 07:15:11 AM
Right, I forgot to assign "undef" back to $_. The command should have been: exiftool -r "-imagedescription<${directory;$_ = s/.* - // ?$_ : undef}" DIR

- Phil
Title: Re: Getting part of folder name into EXIF description
Post by: slashedzero on January 08, 2017, 09:39:27 AM
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!)
Title: Re: Getting part of folder name into EXIF description
Post by: devoid on January 06, 2019, 04:48:21 AM
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!
Title: Re: Getting part of folder name into EXIF description
Post by: Phil Harvey on January 06, 2019, 08:25:05 AM
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 Title: Re: Getting part of folder name into EXIF description Post by: devoid on January 06, 2019, 12:03:44 PM 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! Title: Re: Getting part of folder name into EXIF description Post by: Phil Harvey on January 06, 2019, 02:41:06 PM 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
Title: Re: Getting part of folder name into EXIF description
Post by: user10101 on March 09, 2019, 05:09:13 PM
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$
Title: Re: Getting part of folder name into EXIF description
Post by: StarGeek on March 09, 2019, 07:54:26 PM
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 (https://exiftool.org/forum/index.php/topic,9606.msg49791.html#msg49791).
Title: Re: Getting part of folder name into EXIF description
Post by: Japester on January 06, 2021, 07:49:36 PM
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?
Title: Re: Getting part of folder name into EXIF description
Post by: Phil Harvey on January 06, 2021, 09:00:37 PM
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
Title: Re: Getting part of folder name into EXIF description
Post by: Japester on January 16, 2021, 12:30:35 AM
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.
Title: Re: Getting part of folder name into EXIF description
Post by: StarGeek on January 16, 2021, 12:48:05 AM
Read Phil's post carefully.  Don't CD to the directory with files, as the directory will be the dot, which stands for the current directory.