read and process "directory" tag

Started by elhiero, February 02, 2023, 07:11:12 AM

Previous topic - Next topic

elhiero

Hello, I would like to get the folder name for jpg photos stored in C:\test2 with recursive search.
As an answer I Wish "test2"

I tried C:\Tool\exiftool -k -r -ext jpg ${directory;[^\\]*$} "C:\test2"

I know that ${directory;[^\\]*$} is not the right way but which is the right one ?

maybe a formatting option for -directory tag, giving 'test2' instead of 'C:\test2' ?

Maybe, my regex was wrong, so I tried C:\Tool\exiftool -k -r -ext jpg ${directory;s/e/f/} "C:\test2"
to get 'tfst2' for example ...


And then, if you can give me a method, i would like to incorporate it in a batch for Windows ...
thanks

Phil Harvey

Try this:

exiftool -r -ext jpg -p "${directory;s(.*/)()}" "C:\test2"

But the directory name will be repeated for each contained image in the directory.  To list each directory only once, do this:

exiftool -r -ext jpg -p "${directory;s(.*/)()}" -if "EndDir()" "C:\test2"

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

elhiero

Thank you very much.
1) I didn't know -p "string" option, where string can be the result of a tag processing, and get tag value at the same time. Is there a place to find documentation about this ?

2) your regex is s(.*/)() ,  why are you using () instead of / / ?
I tried [^\\]*$ in regex101 sandbox, it's ok, selecting folder name at the end of the path.
Is there a special syntax in exiftool for regex ?


Here is an example of files structure to explain my final goal :
C:\photos\europe\ ... a lot of photos from France, Italy, Sweden...
C:\photos\asia\ .... a lot of photos of China, Japan ...

I would like to create a text file named "descript.ion" containing
"europe" france italy sweden
"asia" china japan
... by selecting folder names and xmp:country (and other metadata) in all photos with exiftool, and then, filter all this, maybe in a batch file, to get no dups lines.

- File managers or catalogers like Total commander or xnview can manage descript.ion files to have a kind of indexing of the folder content.
Format is : "folder" keyword1 keyword2 ...
or "file" keyword
with relative paths for folder or file

I didn't want to make you do all the work, but I wanted to try to learn step by step .
But if you want to do all the work .......  ;D

Phil Harvey

Quote from: elhiero on February 02, 2023, 09:18:17 AMThank you very much.
1) I didn't know -p "string" option, where string can be the result of a tag processing, and get tag value at the same time. Is there a place to find documentation about this ?

See the -p option in the application documentation.

Quote2) your regex is s(.*/)() ,  why are you using () instead of / / ?

You can choose whatever delimiters you want for a substitution expression.  I didn't use "/" because the expression contained a "/" which I would have had to escape, and "s(.*/)() is maybe easier to read than "s/.*\///".

QuoteIs there a special syntax in exiftool for regex ?

No.  It uses regular Perl syntax.

QuoteBut if you want to do all the work ....

It really isn't much work if this is what you want to do:

exiftool -r -ext jpg -p "${directory;s(.*/)()} $xmp:country" "C:\test2" | sort | uniq > descript.ion

but you will need the "sort" and "uniq" utilities, and I don't know if Windows has these.

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

elhiero

#4
wonderful, I'm almost at the end.

For Win7

C:\Tool\exiftool  -k -m -r -ext jpg  -p "\"${directory;s(.*/)()}\" $xmp:state" "I:\Photos\2012" > result.txt
type result.txt | powershell -nop "$input | sort -unique" > descript.ion
(I'm using xmp:state for now)

Very few lines of descript.ion file still need to be manually modified before I insert it in it's 2012 folder.
"asia" japan
"asia" china
instead of "asia" japan china

I had read the documentation of -p translated into French but it was a headache  ;D
I was trying to add -xmp:state and I would never have found the idea of including the other tag in the quotes without your help.
I hope all this can help other users

StarGeek

Quote from: Phil Harvey on February 02, 2023, 09:29:26 AM
Quote2) your regex is s(.*/)() ,  why are you using () instead of / / ?

You can choose whatever delimiters you want for a substitution expression.  I didn't use "/" because the expression contained a "/" which I would have had to escape, and "s(.*/)() is maybe easier to read than "s/.*\///".

See Leaning toothpick syndrome

Quotebut you will need the "sort" and "uniq" utilities, and I don't know if Windows has these.

For uniq, this SuperUser answer lists a PowerShell command, but there can be other problems when using exiftool on PS.

There is also an answer that points to installing Git for Windows, which might have ports of them.  I haven't check those to make sure.

Myself, I use UnixUtils which are ports of a large number of unix commands.
"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

elhiero

#6
Thank you StarGeek

ChatGPT wrote a bat, to process my previous output file for the last step of the job : (not tested yet, I'll come back in some hours)

So, the whole method to generate XnViewMP compatible decript.ion file by reading any metadata is done.
(XnViewMP database does not handle state, city, ...) but through descript.ion files , it is now easy with quick search field.
I can now describe the content of my photo folders by extracting data with exiftool.

@echo off
setlocal enabledelayedexpansion

rem Initialize variables
set firstColumn=""
set secondColumn=""
set output=""
if exist descript.ion ren descript.ion description.old
rem Loop through each line of the text file
for /f tokens^=1-2^ delims^=^" %%a in (file.txt) do (
   rem Store the first column
   set currentFirstColumn=%%a
   rem Store the second column
   set currentSecondColumn=%%b
   rem If the first column is different from the previously stored first column
   if not "!firstColumn!"=="!currentFirstColumn!" (
      rem If the previously stored first column is not empty
      if not "!firstColumn!"=="" (
         rem Write the previous line with stored values
         echo "!firstColumn!" !secondColumn!>> descript.ion
      )
      rem Store the new first column
      set firstColumn=!currentFirstColumn!
      rem Reset stored second column
      set secondColumn=!currentSecondColumn!
   ) else (
      rem Add the second column to stored second column
      set secondColumn=!secondColumn! !currentSecondColumn!
   )
)

rem Write the last line
echo "!firstColumn!" !secondColumn!>> descript.ion

endlocal