ExifTool Forum

ExifTool => Newbies => Topic started by: Stephen Marsh on November 16, 2017, 01:38:35 AM

Title: Parent Folder/Directory Name as Keyword
Post by: Stephen Marsh on November 16, 2017, 01:38:35 AM
I believe that I have the following regex working correctly for a Mac, such as:

/Users/username/Desktop/Smeg

exiftool '-Subject<${directory;s/\/$|\/|\d+|\w+\///g}' '/Users/username/Desktop/Smeg/File.jpg'

Which results in the keyword "Smeg" being applied.

All good so far!

However, with Windows, I used the following:

exiftool "-Subject<${directory;s/.+\\\b|\\//g}" "C:\Users\Username\Desktop\Smeg\File.jpg"

However, the result was:

C:/Users/Username/Desktop/Smeg

So something wrong with my regex (which tested OK outside of ExifTool).

I am of course looking for the most flexible/robust regex possible in each case, which may not be what I have come up with!


EDIT - I have just tried the following with no luck:

exiftool "-Subject<${directory;s/(?:.+\\\b)|(.+)(?:\\)/$1/g}" "C:\Users\Username\Desktop\Smeg\File.jpg"

Title: Re: Parent Folder/Directory Name as Keyword
Post by: StarGeek on November 16, 2017, 02:43:23 AM
Internally, directory names will have slashes, not backslashes, even on Windows.   You can see this by listing the Directory pseudo-tag for a file.  It's also more consistent across platforms.  Just change the backslashes in your regex.

exiftool "-Subject<${directory;s/.+\/\b|\///g}" "C:\Users\Username\Desktop\Smeg\File.jpg"

Title: Re: Parent Folder/Directory Name as Keyword
Post by: Stephen Marsh on November 16, 2017, 03:20:12 AM
Really?

Smegging smeg!

Thanks StarGeek... I'll have to absorb that I think... Is that a PERL thing, the regex testers did not indicate any errors?
Title: Re: Parent Folder/Directory Name as Keyword
Post by: Phil Harvey on November 16, 2017, 07:02:30 AM
Perl accepts either forward or backward slashes in Windows, and for consistency with other platforms ExifTool chooses to use the forward slash.

- Phil
Title: Re: Parent Folder/Directory Name as Keyword
Post by: StarGeek on November 16, 2017, 11:58:28 AM
Quote from: Stephen Marsh on November 16, 2017, 03:20:12 AM
the regex testers did not indicate any errors?

I didn't actually test it, but it really isn't the way I would go.  You have to know your own data, but since, for example, I occasionally have a leading non-word character (often an exclamation point to change sorting) in my directory names, that would break the \b boundary position in yours.

I would go with:
${directory;s/.*\/([^\/]*$)/$1/}
Title: Re: Parent Folder/Directory Name as Keyword
Post by: Stephen Marsh on November 16, 2017, 12:23:53 PM
Thanks StarGeek, you rock! This regex is both cross platform and more robust than my newb attempt.