Using parent folder name as a output filename

Started by latsakari, February 12, 2024, 06:06:43 AM

Previous topic - Next topic

latsakari

Hi,

I list some data from pictures to file and it works fine, but I would like to use parent folder name as filename automatically.

This works:
exiftool -k -r -ext JPG  -EXIF:Artist -Comment -EXIF:Model -copyright -filename -csv . > Kari.txt

I tried this but it do not work:
exiftool -k -r -ext JPG  -EXIF:Artist -Comment -EXIF:Model -copyright -filename -csv . > ${directory;s/.*\/([^\/]*$)/$1/}.txt

How to use folder name as output filename? I don't want to use static name.


Phil Harvey

You can't do what you want with the -csv option because it can only produce one output file.

But you could use -p instead with a command something like this:

exiftool -k -r -m -ext jpg -w+ %-1:D.txt -p "$directory,$filename,$exif:artist,$comment,$exif:model,$copyright" .

-w+ is necessary to combine output for multiple files, but this will add to any existing .txt files if they already exist.

I've changed your columns around a bit because SourceFile isn't automatically generated with -p.  Also, I've added -m to output blank entries if any tag is missing (instead of skipping that line).

If there is a chance that Artist, Comment or Copyright contains a comma, then you will need to add quotes.

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

latsakari

Hi,
Thanks Phil for fast answer.

I tried your script and I got many txt files but not one combined file which name is parent folder name.
I had 10 *.jpg file in folder .../Vuosi2024/*.jpg and I got 10 *D.txt files, but not one file named Vuosi2024.txt
I use windows bat file.

Br, Kari

Phil Harvey

Hi Kari,

What was the exact command you used, and what version of ExifTool are you running?

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

latsakari

#4
I just copied your script.
exiftool -k -r -m -ext jpg -w+ %-1:D.txt -p "$directory,$filename,$exif:artist,$comment,$exif:model,$copyright" .

I use 12.76

-Kari

Phil Harvey

Hi Kari,

I don't know what the problem is.  This works for me (with single quotes instead because I'm on Mac).  Here I have a couple of images in directories named "tmp" and "tmp2", and 2 output .txt files are created:

> exiftool -r -m -ext jpg -w+ %-1:D.txt -p '$directory,$filename,$exif:artist,$comment,$exif:model,$copyright' .
    3 directories scanned
    3 image files read
    2 output files created
> ls
tmp tmp.txt tmp2 tmp2.txt
> cat tmp.txt
./tmp,b.jpg,,,Canon EOS 5D Mark IV,
./tmp,a.jpg,,a comment,Canon EOS 5D Mark IV,
> cat tmp2.txt
./tmp2,b.jpg,,,Canon EOS 5D Mark IV,
> ls -lR .
total 16
drwxr-xr-x  5 phil  staff  160 12 Feb 08:43 tmp
-rw-r--r--  1 phil  staff   79 12 Feb 08:43 tmp.txt
drwxr-xr-x  3 phil  staff   96 12 Feb 08:43 tmp2
-rw-r--r--  1 phil  staff   37 12 Feb 08:43 tmp2.txt

./tmp:
total 800
-rw-r--r--@ 1 phil  staff  204015 12 Feb 08:43 a.jpg
-rw-r--r--  1 phil  staff  204004 12 Feb 08:43 b.jpg

./tmp2:
total 400
-rw-r--r--  1 phil  staff  204004 12 Feb 07:41 b.jpg

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

latsakari

#6
Thank's for testing

I had 2 folders Test1 and Test2
and 3 images
Folder Vuosi2024/Test1: 1.JPG, 2.JPG
Folder Vuosi2024/Test2: 3.JPG

Parent folder is Vuosi2024 as you see.


I tested again and result are:

exiftool -k -r -m -ext jpg -w+ %-1:D.txt -p "$directory,$filename,$exif:artist,$comment,$exif:model,$copyright" .

    4 directories scanned
    3 image files read
    3 output files created    <------ I GOT THREE FILES
-- press ENTER --

I got three txt files:
File 1D.txt contains text: ./Vuosi2024/Test1,1.JPG,Kari,Comment 1,NIKON Z 6_2,©Copyright 2024, Kari
File 2D.txt contains text: ./Vuosi2024/Test1,2.JPG,Kari,Comment 2,NIKON Z 6_2,©Copyright 2024, Kari
File 3D.txt contains text: ./Vuosi2024/Test2,3.JPG,Kari,Comment 3,NIKON Z 6_2,©Copyright 2024, Kari

BUT if I run command again I'll get
    4 directories scanned
    3 image files read
    0 output files created
    3 output files appended
-- press ENTER --
The script appends same text again to same eg. 1D.txt file.



I need only one file (in this case) Vuosi2024.txt which contains lines eg:
./Vuosi2024/Test1,1.JPG,Kari,Comment 1,NIKON Z 6_2,©Copyright 2024, Kari
./Vuosi2024/Test1,2.JPG,Kari,Comment 2,NIKON Z 6_2,©Copyright 2024, Kari
./Vuosi2024/Test2,3.JPG,Kari,Comment 3,NIKON Z 6_2,©Copyright 2024, Kari

That's why i tried to use -csv command or -T command but I didn't success to create automatically file name from folder name "Vuosi2024".

My aim was to create one file (for Excel), which contains comments (image descriptions) and Camera and photographer data etc.
With Excel I can find a picture which have text eg. "Paris 1985" and get results picture 1001225, picture 1001226...

I have maybe about 450000 pictures. So need to have some way to find correct pictures

I'll continue to find solution

- Kari

Phil Harvey

I didn't notice you were using a .bat file.  You need to double the "%" because it is special in a .bat file:

exiftool -k -r -m -ext jpg -w+ %%-1:D.txt -p "$directory,$filename,$exif:artist,$comment,$exif:model,$copyright" .

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

latsakari

Now it works perfectly.

THANK YOU Phil!

You and Exiftool are awesome.

- Kari