Set Title field from substring of filename?

Started by faj2323, February 13, 2013, 08:51:03 PM

Previous topic - Next topic

faj2323

Hi,

My file naming system goes like this:

031017-027 Trail near Jharkot.jpg

where 031017 = 17 Oct 2003, and 027 = 27th photo taken that day.

Up till now I've used EXIFTOOL in a batch file to populate the Title field with the filename:

EXIFTOOL *.jpg "-Title<Filename"

and then manually editing out the date/serial number information.

As the numerical prefix is always 11 characters I've wondered if it's possible just to select a substring of the file name.

Looking at some of the documentation, specifically the -w option, what I'm wanting is (conceptually) like this:

EXIFTOOL *.jpg "-Title<Filename%.11f"

Is this possible?

Alternatively, reading this forum suggests that I might need a user-defined Basename tag - if so, what's the appropriate line I need to achieve this (I don't understand Perl, unfortunately), and how do I call the function in a batch file?

Thanks in advance.

Phil Harvey

#1
The BaseName user-defined tag is one of the examples in the sample config file, so you could use that if you want, with a command like this:

exiftool "-title<basename" -ext jpg .

Another alternative is to use the new advanced formatting option that was introduced in ExifTool 9.15, and a command like this:

exiftool "-title<${filename;s/\.[^.]*$//}" -ext jpg .

Both techniques will remove the extension for any length of file name.

- Phil

Edit: Changed regex to only remove the last extension
...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 ($).

faj2323

Sorry, over my head (but thanks for your prompt reply, all the same).

Will have a go at the second method, and just downloaded the latest version of ExifTool to use the new advanced formatting options.

I'm supposing, then, that the section under -p in the Application Documentation is where I should be looking - where it says 'An advanced formatting feature allows arbitrary Perl expressions to be applied...'

In the example I originally posted, for the file called '031017-027 Trail near Jharkot.jpg' I want the Title tag to read 'Trail near Jharkot'.

Despite not having a clue about Perl, I've been able to gather that the s/ statement is a Perl substitution which removes the extension (that part works fine), but I also want to remove the first 11 characters (the 'serial number' of the filename). A Perl command to do this is something like substr(variable, 11) but I can't figure out how to incorporate this into the command line. Thanks again.


Phil Harvey

#3
Quote from: faj2323 on February 14, 2013, 07:33:02 PM
Sorry, over my head (but thanks for your prompt reply, all the same).

If you are talking about the config file, all you do is put the sample config file in your home directory or the same directory as ExifTool and rename it to ".ExifTool_config".  (The only tricky part is the rename, for which you'll have to use the "rename" command on the command line because the Windows GUI doesn't let you begin a file name with a ".".)

QuoteI'm supposing, then, that the section under -p in the Application Documentation is where I should be looking - where it says 'An advanced formatting feature allows arbitrary Perl expressions to be applied...'

Yes.

QuoteIn the example I originally posted, for the file called '031017-027 Trail near Jharkot.jpg' I want the Title tag to read 'Trail near Jharkot'.

Sorry, I didn't read your first post carefully enough.  So the config file option won't do what you want anyway.  Try this:

    "-title<${filename;s/^.{11}(.*)\..*$/$1/}"

another way (using substr) would have been:

    "-title<${filename;$_=substr($_,11);s/\.[^.]*$//}"

- Phil

Edit: Changed regex to only remove the last extension
...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 ($).

faj2323

Hi Phil,

Thanks for that, much appreciated. Your solution works a treat and will save me a lot of double entry.