ExifTool Forum

ExifTool => Newbies => Topic started by: rafdilor on January 04, 2016, 09:35:19 AM

Title: Write in -TAG the sub-folder name (where the file is stored)
Post by: rafdilor on January 04, 2016, 09:35:19 AM
Hello, first of all thank you for this very useful tool!
I have used the tool for little changes to my files, but now, I would like to add a TAG to all my pics stored in a directory and all its sub-directory.

The problem is that I need to write in a TAG (e.g.: -ImageDescription) the subfolder name (or a part of that) where the file is stored.
My typical subfolder name is '/2015-12-31 food at city center' (I would like to write in the TAG 'food at city party' or '2015-12-31 food at city center').
Due to huge numbers of files, I don't need to preserve the original file (I have a copy in other locations and I don't want duplicate the use of disk space).
My OS is OS X.
Should I (try to) write a bash script? Can I use a single efixtool command?
Could you help me?

Thank you in advance for your help!  :)

Raffaele
(sorry for my english, I am from Italy)
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: Phil Harvey on January 04, 2016, 10:05:55 AM
Hi Raffaele,

It is a bit tricky to take only part of the directory name, but this can be done using the advanced formatting feature with a command something like this:

exiftool "-imagedescription<this is the folder: ${directory;my @a=split m(/);$_=$a[-1]}" -r DIR

Here I split the Directory into an array of folder names and return the value of the last one in the array.

- Phil
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: rafdilor on January 04, 2016, 02:06:53 PM
Hi Pil, thank you for your support.
Unfortunately this command:
exiftool '-imagedescription<this is the folder: ${directory; my @a=split m(/); $_=$a[-1]}' -r /Users/rafdilor/Documents/photo

returns this error:
-bash: ${directory; my @a=split m(/); $_=$a[-1]}': bad substitution

I'm trying to find out why...

Thank you again,
Raffaele
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: Phil Harvey on January 04, 2016, 02:12:52 PM
Hi Raffaele,

Use ASCII single quotes (') instead of the fancy ones that you used ('').

- Phil
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: rafdilor on January 04, 2016, 02:24:09 PM
 :-[ it was my fault!

now works perfectly :)

Thank you again Phil

Raffaele
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: rafdilor on January 05, 2016, 09:09:02 AM
Hi,
just an update...

Quote from: rafdilor on January 04, 2016, 09:35:19 AM
[cut]
My typical subfolder name is '/2015-12-31 food at city center' (I would like to write in the TAG 'food at city party' or '2015-12-31 food at city center').
[cut]

exiftool '-imagedescription<this is the folder: ${directory; my @a=split m(/); $_=substr($a[-1],11,)}' -r /Users/rafdilor/Documents/photo

works and return the only the part after 11 characters ('2015-12-31 ')

Raffaele
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: FunkyB on August 26, 2017, 09:19:42 AM
I've been trying to do almost exactly the same this as the OP, so I'm replying here instead of starting a new thread. It seems like a simple task, but I wouldn't have been able to do it without this information so I wanted to say thanks and add to the update. My directory structure is the same as the OP's, including the dates in the directory names. That magic string argument after directory was the key - I don't know the syntax used except that it works. I'm using Exiftool 10.61 on Windows 10.

The only issue I have is that a few directories have the title in Japanese, and those give the error "Warning: Some character(s) could not be encoded in Latin - ..." and they show up as gibberish in the output. There aren't too many so I'm fixing them manually, but if there is a way to avoid that could someone tell me how?

Also, I get a lot of minor warnings about "Possibly incorrect maker notes offsets", but I'm guessing it's safe to ignore them.

Here are the commands I came up with that could be useful if your photos are organized by directory and you're now looking to tag them. I saved them as .bat files and just drag the folders I want to process onto them in the Explorer and it works well.

1.  Strips the first 11 characters (date).  *NOTE: there should be NO space between "directory;" and "my @split..."
exiftool "-keywords<${directory;my @a=split m(/); $_=substr($a[-1],11,)}" -overwrite_original -P -r %1
@pause


2a. Tag with the containing directory name as-is:
exiftool "-keywords<${directory;my @a=split m(/);$_=$a[-1]}" -overwrite_original -P -r %1
@pause


2b. Tag with the containing directory name as-is (different code that gives me the same result):
exiftool "-keywords<${directory;s(/$)();s(.*/)()}" -overwrite_original -P -r %1
@pause


I had no idea the metadata in images was so much more complicated than in music files, but it makes me appreciate this tool all the more.
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: Phil Harvey on August 26, 2017, 10:32:41 AM
The problem is that IPTC:Keywords uses Latin encoding by default.  Add this to your command to write as UTF8:

-codedcharacterset=utf8

This is explained in detail in the IPTC section of FAQ 10 (https://exiftool.org/faq.html#Q10).

- Phil
Title: Re: Write in -TAG the sub-folder name (where the file is stored)
Post by: FunkyB on August 26, 2017, 09:50:29 PM
-codedcharacterset=utf8

That works perfectly, thanks!