Author Topic: Replace city name in metadata  (Read 2173 times)

Tom Cunningham

  • Jr. Member
  • **
  • Posts: 25
Replace city name in metadata
« 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.

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 15346
    • ExifTool Home Page
Re: Replace city name in metadata
« Reply #1 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):

Code: [Select]
-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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Tom Cunningham

  • Jr. Member
  • **
  • Posts: 25
Re: Replace city name in metadata
« Reply #2 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?

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 15346
    • ExifTool Home Page
Re: Replace city name in metadata
« Reply #3 on: February 06, 2017, 07:23:05 AM »
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 "=".

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Tom Cunningham

  • Jr. Member
  • **
  • Posts: 25
Re: Replace city name in metadata
« Reply #4 on: February 06, 2017, 11:12:20 AM »
Change all the "+=" to just "=".

Worked like a champ, thanks Phil!

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?

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 15346
    • ExifTool Home Page
Re: Replace city name in metadata
« Reply #5 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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Tom Cunningham

  • Jr. Member
  • **
  • Posts: 25
Re: Replace city name in metadata
« Reply #6 on: February 06, 2017, 12:57:52 PM »
So the -=/= technique is much better if it works for you.

Sounds good, Phil, thanks again.