ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: frereroy on October 25, 2012, 09:50:22 AM

Title: Help in adding path information as keywords
Post by: frereroy on October 25, 2012, 09:50:22 AM
Hello,

I would like to add the names of all the directories in the path leading to my jpgs as IPTC core keywords (separated by a "',")

I ideally I would like to drop a directory onto Exiftool and it would add recursively all the directory names that it finds in leading to the files as keywords.

Thanks for any help.

Title: Re: Help in adding path information as keywords
Post by: Phil Harvey on October 25, 2012, 10:45:40 AM
This config file will allow you to do what you want

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        DirTree => {
            Require => 'Directory',
            ValueConv => q{
                # split directory into components
                my @parts = split '/', $val;
                # remove empty first component (if "/" was the first character)
                shift @parts if @parts and not $parts[0];
                return \@parts;
            },
        },
    },
);
1;  #end


with this command

exiftool "-keywords<dirtree" DIR

See the sample config file (https://exiftool.org/config.html) for help activating this config file.

- Phil
Title: Re: Help in adding path information as keywords
Post by: frereroy on October 30, 2012, 01:32:32 PM
Thanks. I have got it working but only when I specify a directory name by replacing "DIR".

It doesn't seem to recursively do the work on all the subdirectories contained under it.

Thanks for any help that you can supply.

Best wishes.
Title: Re: Help in adding path information as keywords
Post by: Phil Harvey on October 30, 2012, 01:34:04 PM
Add the -r option to recurse into sub-directories.

- Phil
Title: Re: Help in adding path information as keywords
Post by: frereroy on October 30, 2012, 01:41:04 PM
That worked. Many thanks.
Title: Re: Help in adding path information as keywords
Post by: waorak on March 05, 2015, 06:09:49 PM
If my path to my image files is Z:\images\holiday\Europe\France\ would adding the following lines to the script use only Europe and France as keywords:

Shift @parts if @parts and not $parts[1];
Shift @parts if @parts and not $parts[2];
Shift @parts if @parts and not $parts[3];

I would think that $parts[1] = "Z:", $parts[2] ="images", $parts[3] = "holiday" and shift would move past those?

Thank you
Russell
Title: Re: Help in adding path information as keywords
Post by: Phil Harvey on March 05, 2015, 07:32:57 PM
Hi Russel,

Close.  But try this:

shift @parts if @parts;
shift @parts if @parts;
shift @parts if @parts;


The "shift" is case sensitive, and you don't want to check to see if the parts are empty before you shift them out of the array (because they won't be).

Alternatively, you can add this line before splitting $val into parts:

$val =~ s(.*holiday[\\/])();

which will remove everything up to and including "holiday\" from the path before splitting the rest into parts.

- Phil
Title: Re: Help in adding path information as keywords
Post by: waorak on March 05, 2015, 08:13:37 PM
Wow, that was quick. Thank you for your help.

Thank you
Russell
Title: Re: Help in adding path information as keywords
Post by: pacman on March 06, 2015, 07:32:44 AM
I'm trying this on a Mac but am only getting "No writable tags set from PATH" error messages.
I've changed the slash to a backslash in this part of the script:

my @parts = split '\', $val;

I've also tried a bunch of other things, but I keep getting the same error message for my files.
I want to add the name of the folder as keywords, so a slight difference from the OP who wants the path as I understand it. And keywords should be added to any existing keywords in the image, not overwrite them.
Title: Re: Help in adding path information as keywords
Post by: Phil Harvey on March 07, 2015, 03:39:28 PM
You can specify a path in Windows with forward slashes when using ExifTool if you want.

To know what is going wrong, I need to see the exact value of the Directory tag when for some of the files in the command you are running.  i.e.)

exiftool -directory DIR

where DIR is the name of the directory you are specifying.

- Phil
Title: Re: Help in adding path information as keywords
Post by: bcossiboom on January 03, 2020, 03:09:00 PM
Hopefully this will be seen since it's so old. I too am having the same problem pacman reported. I've tried several variations with no luck. I didn't see any additional traffic after your response so I'm not sure if this was solved. I'm sure it's something simple I'm missing, but it's driving me crazy.

Brians-MacBook-Pro:Temp 2 bcossiboom$ exiftool "-keywords+<DirTree" * -r
Warning: No writable tags set from Shooter/GM032119-0186.jpg
Warning: No writable tags set from Shooter/GM032119-0282.jpg
Warning: No writable tags set from Shooter/GM032119-0241.jpg
    1 directories scanned
    0 image files updated
    3 image files unchanged

Brians-MacBook-Pro:Temp 2 bcossiboom$ exiftool -directory *
======== Shooter/GM032119-0186.jpg
Directory                       : Shooter
======== Shooter/GM032119-0282.jpg
Directory                       : Shooter
======== Shooter/GM032119-0241.jpg
Directory                       : Shooter
    1 directories scanned
    3 image files read
Title: Re: Help in adding path information as keywords
Post by: StarGeek on January 03, 2020, 03:24:08 PM
For your first command, did you copy the DirTree config to your .exiftool_config file.  You have to do either that or create a separate config file and use the -Config option (https://exiftool.org/exiftool_pod.html#config-CFGFILE).

Copy the code in the second post (https://exiftool.org/forum/index.php?topic=4513.msg21444#msg21444).  Open a text editor (not a word processor).  Paste the code into and save it into the same directory as exiftool.  Give it an obvious name such as DirTree.config  Then run
exiftool -config DirTree.config "-keywords+<DirTree" -r /path/to/directory
Title: Re: Help in adding path information as keywords
Post by: bcossiboom on January 04, 2020, 07:02:09 AM
Thank you for the speedy reply!

I'm still missing a critical step somewhere b/c ExifTool isn't seeing the DirTree.config file. It now exists everywhere (e.g. my home directory, the root directory with the ExifTool Unix Executable, /usr/local/bin/lib, /usr/local/bin/lib/Image, /usr/local/bin/lib/Image/ExifTool/) and I still can't get it to work. I read in other forum posts that I could specify the config file location, but that caused more problems/errors (not sure why).

I went back through the example.config instructions and I've followed them to the letter (and then did it again just to make sure). I've also reinstalled ExifTool and then repeated the steps above. I'm sure it's something simple that I'm missing, but it's eluding me.
Title: Re: Help in adding path information as keywords
Post by: Phil Harvey on January 04, 2020, 08:34:57 AM
There is a format error in your config file.  Attach the config file you are using and we'll be able to help.

In your first screenshot the config file was found, but it contains errors.

- Phil
Title: Re: Help in adding path information as keywords
Post by: bcossiboom on January 04, 2020, 08:51:19 AM
Attached as requested. Thank you!
Title: Re: Help in adding path information as keywords
Post by: bcossiboom on January 04, 2020, 08:58:24 AM
Attached as requested. I wasn't able to upload the .DirTree.config file, so I've attached the file used to create it. Thank you!
Title: Re: Help in adding path information as keywords
Post by: Phil Harvey on January 04, 2020, 09:46:04 AM
Wow.  There you go.  The file is RTF format.  The config file must be plain text.  Try the attached version.

- Phil
Title: Re: Help in adding path information as keywords
Post by: bcossiboom on January 04, 2020, 09:57:39 AM
That did the trick - THANK YOU!!!