ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Tom Cunningham on February 05, 2017, 03:59:51 PM

Title: Replace city name in metadata
Post by: Tom Cunningham on February 05, 2017, 03:59:51 PM
I would like to replace the city name in all locations where it might be stored (City, Location, LocationShownCity), but only if it contains a particular value.  I've used the following on a subset of files:

exiftool -overwrite_original -api "Filter=s/Harper Acres/Keene/g" -tagsfromfile @ -all:all .

but this changes all strings throughout the metadata.  I need to fine-tune it so that only tags with "Harper Acres" as the exact value will be changed.
Title: Re: Replace city name in metadata
Post by: Phil Harvey on February 05, 2017, 04:10:41 PM
Is it a substring or the exact string that you want replaced?  If the exact string, then the following argfile ("my.args") may do what you want (assuming those are the correct tag names you want replaced):

-city-=Harper Acres
-city+=Keene
-location-=Harper Acres
-location+=Keene
-locationshowncity-=Harper Acres
-locationshowncity+=Keen


And the command would be

exiftool -@ my.args DIR

Alternatively, you could just specify the tags you want changed instead of -all:all, but then the files will be updated even if nothing is changed.  The -=/+= technique will only update the file if it finds and changes some of the values.  It is also quicker because it can be done in a single pass (as opposed to first reading the file then copying the values back again).

- Phil
Title: Re: Replace city name in metadata
Post by: Tom Cunningham on February 05, 2017, 11:43:09 PM
When I run this, I get the following warnings:

Warning: Shift value for IPTC:City is not a number
Warning: Shift value for XMP-iptcCore:Location is not a number

and the city-related tags are gone (I suppose as a result of the -=).  Yes, and to answer your question I did want an exact match, not a substring.

Is there a way to select the city tags based on an exact match?  For example, if I wanted to modify all tags that contained only and exactly "Harper Acres", and City, Location, and LocationShownCity were the only tags with that exact value, is there a way I could modify only those tags?
Title: Re: Replace city name in metadata
Post by: Phil Harvey on February 06, 2017, 07:23:05 AM
Quote from: Tom Cunningham on February 05, 2017, 11:43:09 PM
When I run this, I get the following warnings:

Warning: Shift value for IPTC:City is not a number
Warning: Shift value for XMP-iptcCore:Location is not a number

Oh, right.  Sorry.

Change all the "+=" to just "=".

Quote from: Tom Cunningham on February 05, 2017, 11:43:09 PM
Is there a way to select the city tags based on an exact match?  For example, if I wanted to modify all tags that contained only and exactly "Harper Acres", and City, Location, and LocationShownCity were the only tags with that exact value, is there a way I could modify only those tags?

That is what my command was supposed to do.  Or do you want to do this without specifying any tag names?  If so, then the API Filter option would be the way, but you would do it like this:

exiftool -overwrite_original -api "Filter=$_=$_ eq 'Harper Acres' ? 'Keene' : undef" -tagsfromfile @ -all:all .

- Phil
Title: Re: Replace city name in metadata
Post by: Tom Cunningham on February 06, 2017, 11:12:20 AM
Quote from: Phil Harvey on February 06, 2017, 07:23:05 AM
Change all the "+=" to just "=".

Worked like a champ, thanks Phil!

Quote from: Phil Harvey on February 06, 2017, 07:23:05 AM
That is what my command was supposed to do.  Or do you want to do this without specifying any tag names?  If so, then the API Filter option would be the way, but you would do it like this:

exiftool -overwrite_original -api "Filter=$_=$_ eq 'Harper Acres' ? 'Keene' : undef" -tagsfromfile @ -all:all .

This looks more like what I want to do, but the undef in the conditional makes me uneasy, so before I get carried away: does the above look for any tag with only and exactly the value "Harper Acres" (e.g. not just a substring) and replace that value with "Keene", otherwise it does nothing?  Is that what the undef is for?
Title: Re: Replace city name in metadata
Post by: Phil Harvey on February 06, 2017, 11:54:09 AM
Hi Tom,

I don't really like the Filter option for this.  It does much more work than necessary because it rewrites all of the unchanged tag values as well.  I thought that setting the value to undef would prevent this, but reading the docs now I see that it doesn't (the value is left unchanged if set to undef).

So the -=/= technique is much better if it works for you.

- Phil
Title: Re: Replace city name in metadata
Post by: Tom Cunningham on February 06, 2017, 12:57:52 PM
Quote from: Phil Harvey on February 06, 2017, 11:54:09 AM
So the -=/= technique is much better if it works for you.

Sounds good, Phil, thanks again.