ExifTool Forum

ExifTool => Newbies => Topic started by: EdH on January 17, 2014, 04:52:05 PM

Title: Copy filename to IPTC:Caption-Abstract, without extension
Post by: EdH on January 17, 2014, 04:52:05 PM
This post started as a question that goes back to a topic that Phil and others contributed to in 2007, and that I found in an Internet search.

After writing some of it, I decided to try a variant of an example in the ExifTool documentation, and lo and behold, it seems to work just fine! I have continued the post for several reasons:
- the information may be useful to other newbies like me;
- I would like to know:
   -- (a) if there is any reason for not using this methodology;
   -- (b) if there is a preferable way to do the same thing.

It seems that lots of people want to name their photo files with text that is meaningful to other humans as to what the photo is about. I am one of them. However, then you upload them to a photo website and the name does not appear (at least not where you want it -- what some sites call the "caption"). This is often because the site provides no way for a user to request that the filename be used as, or copied to, a caption on the website, so you have to write new captions for your photos. Irritating! However, if there is content in the IPTC:Caption-Abstract field, some sites will use it as a caption, so it can be very useful to copy the filename (assuming it is meaningful) into that field.

Almost 7 years ago, Phil wrote that the following command will do the copying of the filename into the IPTC:Caption-Abstract field for each file:

    exiftool "-iptc:caption-abstract<filename" -r DIR

where DIR is the directory path to the photos you want processed, and -r causes the command to process all subdirectories also. (-r is optional; DIR could be another form of identification of the file(s) to be processed.)

That's nice, but it copies the entire filename, including the ".jpg" or other extension. I would like to get rid of the ".jpg" extension (that's the only one of concern to me).

I tried a variant of an ExifTool documentation example that I thought might work, as follows:

    exiftool "-iptc:caption-abstract<${filename;s/.jpg//}" "My interesting filename.jpg"

Sure enough, it worked, and I had "My interesting filename" in the caption field.

The deletion of the ".jpg" is done by the Perl "substitute" function "s/yyy/zzz/" where yyy is the text to be replaced by zzz. In the example, the replacement is nothing, thus effectively deleting the ".jpg". (Search for "Perl" in the documentation.)

Caution: This example only replaces the first occurrence of ".jpg", but there normally would only be one.

Phil or someone in the know, please advise on (a) and (b) above.

Thanks!
Title: Re: Copy filename to IPTC:Caption-Abstract, without extension
Post by: Phil Harvey on January 17, 2014, 06:49:17 PM
Hi Ed,

Very good.  This is the way to do it.  But I would tweak the substitution expression in 3 subtle ways to make it more robust:

    "-iptc:caption-abstract<${filename;s/\.jpg$//i}"

The differences are:

1) A "." matches any character in the expression. "\." must be used to match a period only.

2) The trailing "$" causes the expression to match only at the end of the string (but as you said, there should be only one ".jpg" in the name anyway).

3) The "i" makes it a case-insensitive match, so a ".JPG" will also be removed.

- Phil
Title: Re: Copy filename to IPTC:Caption-Abstract, without extension
Post by: EdH on January 17, 2014, 07:22:25 PM
Thanks for the robustness, Phil.

I should have thought of those things from using regular expression engines in other contexts -- I just went with the simple thing that first worked. (It's been 10+ years since I played around a bit with Perl.)

It really does seem -- as others have commented in many other forums -- that ExifTool enables one to manipulate photo metadata in any way one can imagine.
Title: Re: Copy filename to IPTC:Caption-Abstract, without extension
Post by: ryerman on January 18, 2014, 11:59:41 AM
Don't forget about the "BaseName" composite tag.
If you install the sample configuration file (https://exiftool.org/config.html) you can use this:
"-iptc:caption-abstract<basename"
Title: Re: Copy filename to IPTC:Caption-Abstract, without extension
Post by: EdH on January 18, 2014, 09:57:27 PM
Thanks, ryerman. I had not looked at the sample configuration file until you mentioned it, but I see that it contains a number of interesting features and techniques.