Add column to output csv file with subdirectory name

Started by jcusack, October 25, 2013, 12:37:40 PM

Previous topic - Next topic

jcusack

Dear All,

I am new to ExifTool (and to this forum) but will be using it a lot to extract information from thousands of camera trap images. At the moment I have organised my images into species specific folders (e.g. Lion, Elephant, etc), which themselves are organised into camera specific folders (e.g. camera 1, camera 2, etc). I therefore have a hierarchy of subdirectories. I would like to extract the required exif information (see code below) from each image within this hierarchy and export to a csv database. However, for each image I would like to add two columns, one with the name of the camera folder and one with the name of the species folder. Is this possible? And how should I modify the code below?

exiftool -csv -r -FileName -TriggerMode -Sequence -EventNumber -DateTimeOriginal -MoonPhase -AmbientTemperature -SerialNumber -InfraredIlluminator -UserLabel /Users/jeremycusack/Desktop/CT_images  > Image_data.csv

Many thanks for your help!

Jeremy

Phil Harvey

Hi Jeremy,

The easiest thing to do would be to add -directory to the list of tags extracted.

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

jcusack

Hi Phil,

Many thanks for your previous answer (I was away for a while so only just saw it). Adding -directory to the tag list adds an extra column with this entire directory string. Although this is helpful, I was rather hoping I'd be able to separate out the folder names contained in the hierarchy into separate columns in my csv file. Does that make sense?

Thanks again,
Jeremy

StarGeek

#3
I don't believe you can do it directly (though I could be wrong), but you can do it with a user defined tag.

I'm assuming that the very last directory in the path is the species name and the second to last is the camera.  For example,
/Users/jeremycusack/Desktop/CT_images/Some/other/directories/Camera/Species

Here's the config file I came up with using my limited perl skills


use File::Basename;
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
MyAnimalDir => {
            Require => 'Directory',
            # Grabs last directory
            ValueConv => '$val = basename($val)',
        },
MyCameraDir => {
            Require => 'Directory',
            # Grabs second to last directory
            ValueConv => '$val = basename(dirname($val))',
        },
    },
);
1;  #end


Using your previous example, the new command would be:

exiftool -config /path/to/MyConfig -csv -r -FileName -TriggerMode -Sequence -EventNumber -DateTimeOriginal -MoonPhase -AmbientTemperature -SerialNumber -InfraredIlluminator -UserLabel -MyCameraDir  -MyAnimalDir /Users/jeremycusack/Desktop/CT_images  > Image_data.csv


Obviously, you would have to change "/path/to/MyConfig" to the full path of where you saved the above config file.

edit: tweaked comment in config
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

jcusack

Many thanks for the config file, it works well and does what I was looking for. Looks like I need to develop some perl skills of my own...

Thanks again!