ExifTool Forum

ExifTool => Newbies => Topic started by: ChrisJ on February 04, 2018, 09:18:53 AM

Title: Extract Parent folder from FileName
Post by: ChrisJ on February 04, 2018, 09:18:53 AM
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
Title: Re: Extract Parent folder from FileName
Post by: Phil Harvey on February 04, 2018, 09:49:36 AM
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
Title: Re: Extract Parent folder from FileName
Post by: ChrisJ on February 04, 2018, 10:32:34 AM
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)
Title: Re: Extract Parent folder from FileName
Post by: ChrisJ on February 04, 2018, 11:06:12 AM
upps. my issue: Filename contains only filename + file extension!
I try now again with "Directory" instead of Filename
Title: Re: Extract Parent folder from FileName
Post by: ChrisJ on February 04, 2018, 11:45:09 AM
@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!
Title: Re: Extract Parent folder from FileName
Post by: Phil Harvey on February 04, 2018, 01:40:57 PM
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