Parent Folder/Directory Name as Keyword

Started by Stephen Marsh, November 16, 2017, 01:38:35 AM

Previous topic - Next topic

Stephen Marsh

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"


StarGeek

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"

"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

Stephen Marsh

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?

Phil Harvey

Perl accepts either forward or backward slashes in Windows, and for consistency with other platforms ExifTool chooses to use the forward slash.

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

StarGeek

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/}
"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

Stephen Marsh

Thanks StarGeek, you rock! This regex is both cross platform and more robust than my newb attempt.