Author Topic: Append string to filename if tag exists  (Read 1626 times)

daniello

  • Jr. Member
  • **
  • Posts: 17
Append string to filename if tag exists
« on: January 06, 2020, 11:22:28 AM »
Hi, I am looking for a way to differentiate between my phones stock cam pictures and those of a google cam mod I use. The only way obvious to me is the XPComment Exif tag that only exists when pictures are taken with stock cam. The tag itself contains chinese characters which cause hickups in another tool I sometimes use - but generally I don't care about the tag's content.

All I'd need to facilitate is to append a "stock" string to the file name when XPComment as such exists.

Is this something ExifTool can to? Could someone point out a command line off the top of the head, that I could fire at a folder?

Cheers,
daniello

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 5538
Re: Append string to filename if tag exists
« Reply #1 on: January 06, 2020, 11:27:17 AM »
To test it out first, try this
exiftool -if "$XPComment" "-Testname=%f Stock.%e" /path/to/Dir

This will list what filename changes exiftool would make to a directory.  If the output looks correct, change Testname to Filename.

Make sure and don't run it multiple times on a directory though, otherwise you'll end up with multiple "Stock"s in the filename.
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

daniello

  • Jr. Member
  • **
  • Posts: 17
Re: Append string to filename if tag exists
« Reply #2 on: January 06, 2020, 11:57:15 AM »
Simulation gave me warnings (file would exist) and this summary:
 1110 files failed condition
    0 image files updated
  327 image files unchanged

Looked good to me.

Run with "Filename" gave errors but renamed nothing .. I have one file named "e" (which I can recover).
Error: 'd:/2020-01-06 Sri Lanka/Daniel/e' already exists

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 18415
    • ExifTool Home Page
Re: Append string to filename if tag exists
« Reply #3 on: January 06, 2020, 12:29:28 PM »
If all files failed the condition, then XPComment didn't exist in the files (or was empty, or had the value "0").  Try this on a single file that you think should have XPComment to see what Exiftool finds:

exiftool -xpcomment FILE

- 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 ($).

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 5538
Re: Append string to filename if tag exists
« Reply #4 on: January 06, 2020, 12:32:38 PM »
Are you running the command in a Windows BAT file?  If so, see FAQ #27.

Otherwise, something is wrong, as that command shouldn't rename something to just "e".

To avoid naming collisions, you can add %c to add a copy number for duplicates. See the -w (textout) option for details.
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

daniello

  • Jr. Member
  • **
  • Posts: 17
Re: Append string to filename if tag exists
« Reply #5 on: January 06, 2020, 11:55:05 PM »
@Phil

My interpretation of the summary was that 327 images did contain the tag (and remained unchanged due to simulation) and 1110 did. Sums up to the total amount of images. That's why it looked fine to me.

@StarGeek

Thanks .. that fixed it. with double %% it works!

daniello

  • Jr. Member
  • **
  • Posts: 17
Re: Append string to filename if tag exists
« Reply #6 on: January 07, 2020, 12:25:36 AM »
In a very similar scenario I would now like to add tag contents to the filename. The tag is called "Software":

exiftool -if "$Software" "-Testname=%%f %%$Software.%%e" "d:\2020-01-06 Sri Lanka\Daniel\gcam\*.jpg"

I assume it something this easy but the variations I had tried don't work. I read about the -tagsFromFile parameter but I don't know if this is what I need to deal with.

StarGeek

  • Global Moderator
  • ExifTool Freak
  • *****
  • Posts: 5538
Re: Append string to filename if tag exists
« Reply #7 on: January 07, 2020, 12:52:26 AM »
See Common Mistake #5c.

In this case you are copying a tag to another tag, not assigning a static value.  So you have to change the equal sign = to a Less/Greater Than sign (< or >). 

Additionally, you have superfluous percent signs.  The percent signs are only used with the special variables such as the %f and %e.  See the -w (textout) option for details.

You may have to add braces to $Software to prevent exiftool from thinking what follows afterwards is part of the tag name.  See the -p (printFormat) option.

So try (braces may be unnecessary, I haven't tested it)
exiftool -if "$Software" "-Testname<%%f ${Software}.%%e" "d:\2020-01-06 Sri Lanka\Daniel\gcam\*.jpg"
Troubleshooting hints:
* When posting, include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).
* Double all percent signs (%) in a Windows batch file.
* If your GPS coords are negative, make sure and set the GpsLatitudeRef and GpsLongitudeRef tags correctly.

daniello

  • Jr. Member
  • **
  • Posts: 17
Re: Append string to filename if tag exists
« Reply #8 on: January 07, 2020, 01:40:08 AM »
thanks .. it seems to work with and without braces.

There is a warning though in both runs:
Warning: [minor] Unrecognized MakerNotes

d:/2020-01-06 Sri Lanka/Daniel/gcam/2019-12-28 11.35.30_daniel.jpg' --> 'd:/2020-01-06 Sri Lanka/Daniel/gcam/2019-12-28 11.35.30_daniel HDR+ 1.0.276119539zdh.jpg'
Warning: [minor] Unrecognized MakerNotes - d:/2020-01-06 Sri Lanka/Daniel/gcam/2019-12-28 11.35.40_daniel.jpg

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 18415
    • ExifTool Home Page
Re: Append string to filename if tag exists
« Reply #9 on: January 07, 2020, 07:05:00 AM »
See the last section of FAQ 15
...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 ($).

daniello

  • Jr. Member
  • **
  • Posts: 17
Re: Append string to filename if tag exists
« Reply #10 on: January 07, 2020, 07:59:07 AM »
Thanks .. I'm good now .. everything worked out well .. all I need now is motivation for sorting all pictures ;-)

I didn't find how to attach a [Solved] to the title.

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 18415
    • ExifTool Home Page
Re: Append string to filename if tag exists
« Reply #11 on: January 07, 2020, 08:14:50 AM »
I didn't find how to attach a [Solved] to the title.

exiftool "-title<$title [Solved]" FILE

hehe (joke)

...but seriously, the forum is configured to not allow editing of posts after 12 hours, so it is generally not possible to edit the title of the first post in a topic.

- 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 ($).