ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: msdobrescu on March 19, 2013, 03:00:23 PM

Title: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: msdobrescu on March 19, 2013, 03:00:23 PM
Hello,

I need to set Iptc:Caption-Abstract to some text that contains part of the filename.
Assuming the file is called "some file 'a' left.jpg", let's say I need to set Iptc:Caption-Abstract to "the left image of 'a'".
How could I achieve this?

Thank you.
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: Phil Harvey on March 19, 2013, 03:16:05 PM
Something like this should do what you want:

exiftool "-iptc:caption-abstract<the left image of ${filename;s/ left.*//}" FILE

where FILE is one or more file and/or directory names.  The substitution expression (s///) deletes everything from " left" onwards in the file name.

The quoting above should work in Windows.  Use single quotes instead if you are on Mac or Linux.

- Phil
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: msdobrescu on March 19, 2013, 03:19:33 PM
Thank you for the fast reply. Assuming I need to scramble a little more the name, is there a way to specify something like "%15f", i.e to take 15 characters from the beginning, 3 from the middle, the last 5 ones, what should I do?
Where could I find the specs for expressions like the one you've proposed?
Are there any regular expressions available?

Thanks.
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: Phil Harvey on March 19, 2013, 03:36:57 PM
Beware: Regular expressions aren't for the faint of heart.  This is considered an advanced feature of ExifTool.

Google for "Perl regular expression" for documentation on this.  (Or even just "regular expression", since other languages use the same syntax.)

To do take 15 characters from the start and 5 from the end you could do this:

   ${filename;s/^(.{15}).*(.{5})\..*?$/some $1 text $2/}

where $1 and $2 would be the first 15 characters and the last 5.  To take ones from the middle I would need to have some way to tell which 3 characters.  The bit after (.{5}) in the expression just matches the file extension.  Note that this expression contains a backslash, which can be tricky to escape in some shells.

- Phil
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: msdobrescu on March 19, 2013, 03:45:49 PM
Thanks,

Regular expressions are not a problem to me, I have used them in TextPad, Notepad++, Visual C, C# and many more. But Perl is totally alien to me.
I think that's the issue. Now, I have a basic sample to start with.

Thank you.
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: msdobrescu on March 20, 2013, 03:54:03 AM
Hello,

I have the following situation:

The text: blah blah lower blah blah g (optional here) left-01.jpg
The regex: /blah blah (lower|upper) blah blah (.{1}) (?:(.*))(left|right).*/

Here: http://www.solmetra.com/scripts/regex/index.php I can test and it shows:

Array
(
   
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: Phil Harvey on March 20, 2013, 07:14:38 AM
This isn't what you expected?:

> exiftool a.jpg -comment='blah blah lower blah blah g (optional here) left-01.jpg'
    1 image files updated
> exiftool a.jpg -p '${comment;s/blah blah (lower|upper) blah blah (.{1}) (?:(.*))(left|right).*/$1|$2|$3|$4/}'
lower|g|(optional here) |left


- Phil
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: msdobrescu on March 20, 2013, 07:39:43 AM
Yes, and I've just found the issue, it was a double quote missing that made the whole command a mess.

Thanks, I have to learn more now.  :)
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: muesli on February 01, 2016, 03:29:08 PM
Hello!

I have a similar problem as mentioned in this topic, maybe someone can help me:
I got about 500 photos of my childhood scanned by my parents. All the description is in the file names. They all have a three digit number followed by two times space, then the description, e.g. "025  Minimundus Klagenfurt.jpg". What do I have to do to get Minimundus Klagenfurt to the caption?
Thank you very much and best regards from Germany,

Müsli
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: StarGeek on February 01, 2016, 04:08:26 PM
Try
exiftool "-Caption-Abstract<${filename;s/^\d{3}  //}" FileOrDir

Try it out on a test file first to make sure it works like you want.
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: muesli on February 02, 2016, 10:57:44 AM
Hello StarGeek,

thank you very much for your reply.
Unfortunately the result was "025  Minimundus Klagenfurt.jpg;s/^\d{3}  //}"
Did I make a mistake?
Thanks again an best wishes,

Müsli
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: Hayo Baan on February 02, 2016, 12:20:22 PM
If you're on a Mac, change the double quotes to single quotes  ;)
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: muesli on February 02, 2016, 12:21:59 PM
Thanks, but my system is Windows  :(
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: StarGeek on February 02, 2016, 02:08:02 PM
Somehow you missed the opening brace.  The command works correctly here.

Notice I was able to replicate your output in the second command where I didn't have the opening brace.

c:\>exiftool "-Caption-Abstract<${filename;s/^\d{3}  //}" "X:\!temp\025  Minimundus Klagenfurt.jpg"
    1 image files updated

c:\>exiftool -caption-abstract "X:\!temp\025  Minimundus Klagenfurt.jpg"
Caption-Abstract                : Minimundus Klagenfurt.jpg

c:\>exiftool "-Caption-Abstract<$filename;s/^\d{3}  //}" "X:\!temp\025  Minimundus Klagenfurt.jpg"
    1 image files updated

c:\>exiftool -caption-abstract "X:\!temp\025  Minimundus Klagenfurt.jpg"
Caption-Abstract                : 025  Minimundus Klagenfurt.jpg;s/^\d{3}  //}

Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: muesli on February 02, 2016, 02:46:16 PM
Thank you StarGeek for your patience!
I did not forget the brace, I used copy and paste as well from your original post as from the new one. Your version with the missing brace produces the same output you had in this case and I have in both cases :(
I tried it with two opening braces but that screwed up things even more ;) I also tried to type the command by myself and not use copy and paste, same result.
Did you also use the windows command line?

Thanks, Müsli
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: StarGeek on February 02, 2016, 03:23:31 PM
What version of exiftool are you using (exiftool -ver)?  I can't remember the exact version the advanced formatting was added but it has to be at least in the 9s.

I'm using a variant cmd line (PowerCmd, which I don't recommend for anyone using Win 8 or higher), but the command works fine in a regular CMD.  Powershell tries to interpret the quoted part as a variable and throws back an error before it even runs :( 
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: muesli on February 02, 2016, 03:45:09 PM
I think you got the point. Exiftool came installed with Geosetter (maybe you know that) and within Geosetter ist says version 10.10. But your command gets me 8.47. Maybe the installation path changed and I use the old one. I will check this out tomorrow and post the result.

All the best, Müsli
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: StarGeek on February 02, 2016, 04:19:41 PM
Geosetter places a copy of exiftool in it's appdata folder (for example, under windows 8.1, it's C:\Users\<USERNAME>\AppData\Roaming\GeoSetter\tools\exiftool.exe or just %appdata%\GeoSetter\tools) (Your post got me to finally add %appdata%\GeoSetter\tools to my PATH env var so now I'll always use the version that Geosetter has installed).

You can figure out where the exiftool you're using by the windows command where:
where exiftool

Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: muesli on February 03, 2016, 09:36:22 AM
The old version which I used was in C:\Program Files (x86)\GeoSetter\tools, with the new one in %appdata% everything works fine  :)
Thank you very much for your help and patience!

Best wishes, Müsli

P.S.: Just in case someone is interested, as written in Geosetter help I put

# The %Image::ExifTool::UserDefined hash defines new tags to be added
# to existing tables.
%Image::ExifTool::UserDefined = (
    # Composite tags are added to the Composite table:
    'Image::ExifTool::Composite' => {
        # Composite tags are unique: The Require/Desire elements list
        # tags that must/may exist, and the keys of these hashes are used
        # as indices in the @val array of the ValueConv expression to
        # derive the composite tag value.  (See the Composite table in
        # Image::ExifTool::Exif for more examples.)
        BaseName => {
            Require => {
                0 => 'FileName',
            },
            # remove the extension from FileName
            ValueConv => 'my $name=$val[0]; $name=~s/\..*?$//; $name',
        },
        # the following examples demonstrate simplifications which may be
        # used if only one tag is Require'd or Desire'd:
        # 1) the Require/Desire lookup may be replaced with a simple tag name
        # 2) "$val" may be used to represent "$val[0]" in the expression
        Extension => {
            Require => 'FileName',
            ValueConv => '$val=~/\.([^.]*)$/; $1',
        },
    },
);


into the .exiftool_config and replaced filename with BaseName in StarGeek's command line to get rid of the .jpg in the caption. Probably there is a better way to do so as I am not familiar with the ExifTool commands but it works ;)
Title: Re: setting Iptc:Caption-Abstract to some text that contains part of the filename
Post by: StarGeek on February 03, 2016, 11:21:54 AM
Quote from: muesli on February 03, 2016, 09:36:22 AM
into the .exiftool_config and replaced filename with BaseName in StarGeek's command line to get rid of the .jpg in the caption. Probably there is a better way to do so as I am not familiar with the ExifTool commands but it works ;)

Using BaseName is probably the best way.  It's easier to work with than the other way of using the advanced formatting and mistakes are less likely to happen.