Author Topic: SetNewValue() not working when field does not already exist  (Read 385 times)

frank

  • Newbie
  • *
  • Posts: 8
SetNewValue() not working when field does not already exist
« on: April 25, 2020, 10:17:30 AM »
Hi Phil,

Sorry to bother you with this.. I'm not a Perl programmer and I just can't get this to work.

I'm using the API to set the value of DateTimeOriginal and DateTimeDigitized and it works like a charm.. as long as the tags already exist in the image file.

Code: [Select]
my $exifTool = new Image::ExifTool;
$exifTool->Options(IgnoreMinorErrors => 1);
$exifTool->SetNewValue(DateTimeOriginal => $value);
$success = $exifTool->WriteInfo($path);

If the tags don't already exist, nothing happens. What I want to do is add the tag if it does not already exist and replace it if it does.

I have found the DelValue & AddValue flags (not sure what you call them in Perl), but the DelValue seems to require the current value (which I'd prefer not reading unnecessary) and the AddValue flag only works on list types.

How do I express the "add or replace" semantics correctly?

Best regards,

Frank

Phil Harvey

  • ExifTool Author
  • Administrator
  • ExifTool Freak
  • *****
  • Posts: 16879
    • ExifTool Home Page
Re: SetNewValue() not working when field does not already exist
« Reply #1 on: April 25, 2020, 08:42:54 PM »
Hi Frank,

The code you posted will write EXIF:DateTimeOriginal even if it didn't already exist, unless there was some error writing the EXIF.  Check the return code from WriteInfo() and $exifTool->GetValue('Error') to see if there were any errors.

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