Extract Parent folder from FileName

Started by ChrisJ, February 04, 2018, 09:18:53 AM

Previous topic - Next topic

ChrisJ

Hello
my filenames look like this
   c:/Folder1/.../FolderX/2003/2003-12 Eventname/2003-12-10_07-50-02_Canon EOS 300D DIGITAL_103_0358_JFR.JPG

I use RegEx to extract "parent folder of the file" + the fixed string Event/ as Prefix
'-subject<Event/${filename;(\d{4}-\d{2} [^\/]*)}'
  result: Event/2003-12-10_07-50-02_Canon EOS 300D
  expected result: Event/2003-12 Eventname

Where is the issue?
According to https://regexr.com the expression is "valid" and extracts the right part ...
ExifTool raises a ...
   Warning: Search pattern not terminated for 'filename'

Many thanks in advance for tipps

Phil Harvey

Hi Chris,

The exiftool advanced formatting expression is a perl expression, not a regular expression.  Try this:

'-subject<Event/${filename;$_ = /(\d{4}-\d{2} [^\/]*)/ ? $1 : undef}'

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

ChrisJ

many thanks for the fast response;

now:no warning :-)

But still the same wrong result; there is still an issue with perl expression
  Result: Event/2003-12-10_07-50-02_Canon EOS 300D
  Expected: Event/2003-12 Eventname

everything seems for me OK ... why this result (hope there is no copy paste issue on my side)

ChrisJ

upps. my issue: Filename contains only filename + file extension!
I try now again with "Directory" instead of Filename

ChrisJ

@all: now I found the right thing (using search now with right keywords) here
https://exiftool.org/forum/index.php/topic,8714.msg44724.html#msg44724

The solution is
   ${directory;s/.*\/([^\/]*$)/$1/}

@Phil: Many thanks for your quick response and for this great piece of software!

Phil Harvey

Great, glad you figured it out.

There is definitely more than one way to do it.  The main difference between the technique you found (using a substitution expression) and my example is that if the expression doesn't match your technique will set the Subject from the full directory name, while the technique I showed won't write anything.

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