ExifTool Forum

ExifTool => Archives => Topic started by: Archive on May 12, 2010, 08:54:32 AM

Title: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by pixelpicker on 2009-03-13 14:53:29-07]

Hello to all - and special to Phil,

I have a problem with a program reading some of my metadata:

if there is a " ' " in the keywords used (line in Oetker's baking powder), the whole image is not accepted.

What I tried upt to now: created .mie file for each image and tried to use the Program InfoRapid to search and replace all " ' " in the files - then later write back the new metadata in the images. Unfortunately the .mie files use a format, that has a lot of " ' " in the encoding, which is visible when you open the mie file in a texteditor. Since I have a lot of images its a hell to search every word on its own to replace it without the '.

My questions now:

1) Is there any program that can handle .mie files in batch? Or

2) Could it even be possible to find a script to do the search and replace with ExifTool itself?Huh (That would be the best.)

Greetings from

pixelpicker Smiley
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by exiftool on 2009-03-13 15:41:06-07]

MIE is a binary-format file, and you could easily corrupt
the file by doing a text search-and-replace.  You could
do this with an XML file if you want (-X option),
but you would still have to replace all "'" with
""" in the XML file(s).

This would be much faster to do with a user-defined tag,
but as it turns out there a problem implementing a translation
of values in list-type tags.  I will see if I can improve this
with the next release.

- Phil
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by exiftool on 2009-03-13 16:01:12-07]

I figured out the problem, and it was with my config file, not exiftool.
It is trickier than I would like to create a user-defined
tag to do this, but here is the config file to do it:

Code:
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyKeywords => {
            Require => 'Keywords',
            ValueConv => q{
                my @list = ref $val ? @$val : ($val); # create a list
                s/'/"/g foreach @list; # substitute quotes for each list value
                return \@list;
            },
        },
    },
);
1;  #end

Then, with this command you can translate single
quotes to double quotes in keywords for all files in directory DIR:

In Windows:

Code:
exiftool "-keywords<mykeywords" -if "$keywords=~/'/" DIR

In Unix and Mac:

Code:
exiftool "-keywords<mykeywords" -if '$'"keywords=~/'/" DIR

These commands will only process images which
have a single quote in the keywords to begin with.

- Phil
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by exiftool on 2009-03-13 16:08:48-07]

One final point.  I was assuming that you wanted to replace
the single quote with double quotes, but re-reading your
post I see you didn't specify what you wanted to replace it
with.  Simply change the character in the "substitute" line of
the config file to replace it with something else, or delete
the double quote character from this line to delete the single
quotes in the keywords.

- Phil
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by pixelpicker on 2009-03-13 17:08:48-07]

Ah Phil - Genious YOU again!!!!!!!!!

THANK YOU.

I'm not a programmer - so if you could tell me:

1) where/how to adapt this config file?

I would appreciate al lot. Yes, I forgot to tell with what should be replaced but as I see from your last post it can be left empty or put a space (which would be best cause some words loose meanig by just erasing the ' (eg. Names like d'Allimonte would become Dallimonte - which isn't correct).

Oooops - it comes out kow, that the problem also occurs with the caption and the headline of the iptc data. Question:

2) Is it possible to create other variables like eg. "MyCaption" and "MyHeadline" and execute the search for a defined sign or word like " ' " over keywords, caption and headline over the images as one command??

Happy to hear from you Smiley

Best greetings from

pixelpicker
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by exiftool on 2009-03-13 17:21:49-07]

Hopefully the
config
file documentation
should answer your questions about how to
install the config file.

You can replace the double quote with a space if you want.  This will work.

Creating any number of user-defined tags is possible.  For non-list tags
the translation is simpler.  But since you want to do a number at once,
I added some extra logic so the user-defined tag will only exist if the
substitution actually did something:

Code:
%Image::ExifTool::UserDefined = (
    'Image::ExifTool::Composite' => {
        MyKeywords => {
            Require => 'Keywords',
            ValueConv => q{
                my @list = ref $val ? @$val : ($val);
                my $changed;
                s/'/ /g and $changed = 1 foreach @list;
                return $changed ? \@list : undef;
            },
        },
        MyCaption => {
            Require => 'Caption-Abstract',
            ValueConv => q{ $val=~s/'/ /g ? $val : undef },
        },
    },
);
1;  #end

With this config file, the command will look like this
(no -if needed):

Code:
exiftool "-keywords<mykeywords" "-caption-abstract<mycaption" DIR

And of course you can do this with as many tags as you want.

- Phil
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by pixelpicker on 2009-03-13 17:29:54-07]

Phil, Phil - you make me happy!!!! Thank you for your quick reply!

I will read though the config file documentation and do everything you wrote - and then post the results.

Till then a happy time for you.

See you later Smiley

Best greetings and thanx! from

pixelpicker
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by pixelpicker on 2009-03-13 22:02:54-07]

Hello again Smiley

BRAVO - Phil! Works like heaven! (and saves me a lot of nerves) - THANK YOU VERY MUCH.

----------------

So here for all the people who need to "Search and Replace"

in Caption, Keywords and Headline of image Metadata:

1) Put the .ExifTool_config in the folder where your exiftool.exe is

- see config
file documentation
and related sides for use of this file.

2) Copy the following Code and put it at the end of the .ExifTool_config before

 1;  #end :

Code:
#---- My Own Tags start --------------------------------------------------------------------

# My own Tags "MyKeywords" & "MyCaption" & "MyHeadline" for Search & Replace
# in Keywords, Caption & Headline:

  %Image::ExifTool::UserDefined = (    
    'Image::ExifTool::Composite' => {
        MyKeywords => {
          Require => 'Keywords',
          ValueConv => q{
            my @list = ref $val ? @$val : ($val);
            my $changed;
            s/SEARCHWORD/REPLACEWORD/g and $changed = 1 foreach @list;
            return $changed ? \@list : undef;
          },
        },
        MyCaption => {
          Require => 'Caption-Abstract',
          ValueConv => q{ $val=~s/SEARCHWORD/REPLACEWORD/g ? $val : undef },
        },
       
         MyHeadline => {
          Require => 'Headline',
         ValueConv => q{ $val=~s/SEARCHWORD/REPLACEWORD/g ? $val : undef },
        },
      },
);

# the command to execute the Search & Replace  of: [SEARCHWORD] with [REPLACEWORD] in Windows
# in Keywords, Caption & Headline for all files in directory DIR looks like this:
# exiftool "-keywords<mykeywords" "-caption-abstract<mycaption" "-headline<myheadline" DIR

#---- My Own Tags end --------------------------------------------------------------------

3) Replace the SEARCHWORD and REPLACEWORD with your own words.

4) Use the command in ExifTool to search and replace.

Greetings from the happy

pixelpicker Smiley
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by pixelpicker on 2009-03-16 11:10:11-07]

Dear Phil,

since this search and replace tag/commad is VERY helpful for all sort of changing metadata I am thinking about making it more generalized.

The idea for that came with your first code you posted:

Code:
exiftool "-keywords<mykeywords" -if "$keywords=~/'/" DIR

You have this if inside, which is very interesting and lead me to the question:

Is it possible to put variables in the s/'/"/g instead of the ' (means searchword) and another for the " (means replaceword) and definde these ones on the commandline, like:

config file:

Code:
s/var1/var2/g foreach @list; # substitute quotes for each list value  

that would be "filled" on the command line with concrete search- and replacewords:

command:

Code:
exiftool "-keywords<mykeywords" -if "$keywords=~/searchword=var1/replaceword=var2 DIR

??

This would mean, one doesn't have to change the config file each time for search and replace but define the search- and replacewords direkt on the command line.

Hoping you are fine. Best greetings from the still happy Smiley

pixelpicker
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by exiftool on 2009-03-16 11:35:52-07]

Interesting idea.  I would consider adding a feature like this if I get
more requests for it in the future.  Usually I try to avoid adding
new and confusing features like this that most people will never
use because ExifTool is already too complicated.  I'm sure you
understand.

- Phil
Title: Re: Search and replace in keywords...
Post by: Archive on May 12, 2010, 08:54:32 AM
[Originally posted by pixelpicker on 2009-03-16 15:23:35-07]

Of course Phil - I understand.

As you know I have no clue in pearl and can't estimate how complex something like the mentioned is. As I see from your answer: it is complex.

So I say THANK YOU for your time and your super solution my solved my problem!

Greetings to you & have a happy time Smiley

pixelpicker