Author Topic: Getting part of folder name into EXIF description  (Read 1726 times)

slashedzero

  • Newbie
  • *
  • Posts: 4
Getting part of folder name into EXIF description
« 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.)

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: Getting part of folder name into EXIF description
« Reply #1 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

slashedzero

  • Newbie
  • *
  • Posts: 4
Re: Getting part of folder name into EXIF description
« Reply #2 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.

slashedzero

  • Newbie
  • *
  • Posts: 4
Re: Getting part of folder name into EXIF description
« Reply #3 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?

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: Getting part of folder name into EXIF description
« Reply #4 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

slashedzero

  • Newbie
  • *
  • Posts: 4
Re: Getting part of folder name into EXIF description
« Reply #5 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!)

devoid

  • Newbie
  • *
  • Posts: 2
Re: Getting part of folder name into EXIF description
« Reply #6 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!

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: Getting part of folder name into EXIF description
« Reply #7 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

devoid

  • Newbie
  • *
  • Posts: 2
Re: Getting part of folder name into EXIF description
« Reply #8 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!

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 14759
    • ExifTool Home Page
Re: Getting part of folder name into EXIF description
« Reply #9 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

user10101

  • Newbie
  • *
  • Posts: 3
Re: Getting part of folder name into EXIF description
« Reply #10 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$

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 2516
Re: Getting part of folder name into EXIF description
« Reply #11 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.
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.