Assign only if a tag already has a value...

Started by busywait, December 15, 2017, 06:07:09 AM

Previous topic - Next topic

busywait

I found an example to only write values in certain situations:
https://exiftool.org/exiftool_pod.html#WRITING-EXAMPLES
Quoteexiftool -comment-= -comment='new comment' a.jpg
Write a new comment only if the image doesn't have one already.

Is there a way to assign -comment='new comment' only if comment *already* had a value?

More specifically, I'd like to do something like this:
exiftool -title='snappy' '-comment-<${comment}' -comment='new comment' a.jpg
Does that make sense? It doesn't work as I hoped, because the -comment assignment is always made.

I have a work-around which is this
exiftool -title='snappy' -execute -if '$comment' -comment='new comment' -common_args a.jpg

I think that my work-around writes the file twice, which I'd like to avoid - is that the simplest option?

Phil Harvey

Your first command didn't work because the -< is not done if Comment doesn't exist.

I suppose, along those lines, something tricky like this could work:

exiftool -title='snappy' '-comment<${comment;$_="new comment"}' a.jpg

Your second command will work, but it won't write Title if Comment doesn't exist.

An alternative is the WriteMode (-wm) feature, which allows you to avoid creating new tags:

exiftool -wm w -title='snappy' -comment='new comment' a.jpg

But this will only write Title if it already exists.  However, it is more efficient than the command above because it doesn't require the extra read pass to extract existing tags.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

busywait

Thanks once more Phil!

I did test my second command, and it seemed to be working (that is, always writes Title, conditionally writes Comment) - did you spot the -execute and -common_args options?

Your tricky solution looks good, but testing that I can't get it to write over an existing value - an error about a "Bareword". Here is the console output that I get:

PS C:\EXIF> .\exiftool.exe -g1 -Title -Comment .\a.jpg
---- XMP-dc ----
Title                           : Dull

PS C:\EXIF> .\exiftool.exe -Title="Snappy" '-Comment<${Comment;$_="Snappy"}' .\a.jpg
Warning: [minor] Tag 'Comment' not defined - ./a.jpg
Warning: No writable tags set from ./a.jpg
    1 image files updated

PS C:\EXIF> .\exiftool.exe -g1 -Title -Comment .\a.jpg
---- XMP-dc ----
Title                           : Snappy

PS C:\EXIF> ./exiftool -Comment=Dull .\a.jpg
    1 image files updated

PS C:\EXIF> .\exiftool.exe -g1 -Title -Comment .\a.jpg
---- XMP-dc ----
Title                           : Snappy
---- File ----
Comment                         : Dull

PS C:\EXIF> .\exiftool.exe -Title="Snappy" '-Comment<${Comment;$_="Snappy"}' .\a.jpg
Warning: Bareword "Snappy" not allowed while "strict subs" in use for 'Comment' - ./a.jpg
    1 image files updated

PS C:\EXIF> .\exiftool.exe -Title="Snappy" '-Comment<${Comment;$_=Snappy}' .\a.jpg
Warning: Bareword "Snappy" not allowed while "strict subs" in use for 'Comment' - ./a.jpg
    1 image files updated

PS C:\EXIF> .\exiftool.exe -Title="Snappy" '-Comment<${Comment;$_=\'Snappy\'}' .\a.jpg

Warning: Error opening file - Snappy/}
Error: File not found - Snappy/}
Warning: [minor] Tag '_' not defined - ./a.jpg
Warning: No writable tags set from ./a.jpg
    1 image files updated
    1 files weren't updated due to errors

PS C:\EXIF> .\exiftool.exe -Title="Snappy" '-Comment<${Comment;$_="Snappy"}' .\a.jpg
Warning: Bareword "Snappy" not allowed while "strict subs" in use for 'Comment' - ./a.jpg
    1 image files updated

PS C:\EXIF> .\exiftool.exe -g1 -Title -Comment .\a.jpg
---- XMP-dc ----
Title                           : Snappy
---- File ----
Comment                         : Dull


Phil Harvey

Quote from: busywait on December 15, 2017, 09:55:36 AM
did you spot the -execute and -common_args options?

Nope.   Missed those.  Yes, running a separate commands will work.

QuoteYour tricky solution looks good, but testing that I can't get it to write over an existing value - an error about a "Bareword".

Ah.  You're on Windows, so you must use double quotes around all arguments and single quotes within.  I thought you were on Mac or Linux because of the quoting in your first post.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

busywait

Grr - I should have tried that, sorry. (Windows 10 Powershell prompt, if that makes any difference - it's much nicer to use than the old CMD prompt.)

The error is now gone, but it doesn't look like my tag is getting written:

PS C:\EXIF> .\exiftool.exe -Title="Snappy" "-Comment<${Comment;$_='Snappy'}" .\a.jpg
Warning: No writable tags set from ./a.jpg
    1 image files updated

PS C:\EXIF> .\exiftool.exe -g1 -Title -Comment .\a.jpg
---- XMP-dc ----
Title                           : Snappy
---- File ----
Comment                         : Dull

Phil Harvey

I'm guessing this is either a PowerShell thing, or you are using a very old version of ExifTool, because it should work.  Is "$" special somehow for PowerShell?  On the Mac:

> exiftool -g1 -Title -Comment a.jpg
---- XMP-dc ----
Title                           : Snappy
---- File ----
Comment                         : dull
> exiftool -Title="Snappy" '-Comment<${Comment;$_="Snappy"}' a.jpg
    1 image files updated
> exiftool -g1 -Title -Comment a.jpg
---- XMP-dc ----
Title                           : Snappy
---- File ----
Comment                         : Snappy
> exiftool -ver
10.68


- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

StarGeek

I think Powershell uses Mac/Linux like variable expansion.  So, try swapping the quotes back.
"It didn't work" isn't helpful. What was the exact command used and the output.
Read FAQ #3 and use that cmd
Please use the Code button for exiftool output

Please include your OS/Exiftool version/filetype

busywait

#7
Thanks Stargeek, Phil,

I'm using exiftool 10.68.

Stargeek, I've already posted the output with the quotes the other way round, so I guess I've tried it both ways now.

Here is the output from the command running in CMD:
Microsoft Windows [Version 10.0.16299.125]
(c) 2017 Microsoft Corporation. All rights reserved.

C:\EXIF>.\exiftool.exe -Title="New Title" "-Comment=${Comment;$_='New Comment'}" a.jpg
    1 image files updated

C:\EXIF>.\exiftool.exe -Title -Comment a.jpg
Title                           : New Title
Comment                         : ${Comment;$_='New Comment'}

C:\EXIF>.\exiftool.exe -Title="New Title" '-Comment=${Comment;$_="New Comment"}' a.jpg
Error: File not found - '-Comment=${Comment;$_=New Comment}'
    1 image files updated
    1 files weren't updated due to errors

C:\EXIF>.\exiftool.exe -Title -Comment a.jpg
Title                           : New Title
Comment                         : ${Comment;$_='New Comment'}


Also, this did not appear to write my value.

I can stick with my "double writing" execute/common_args work-around for now, but I am interested if you spot that I just typed something wrong :)

Phil Harvey

You wrongly used "=" instead of "<" in these latest commands. (Common mistake 5c)

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

busywait

 :-[ You're right.

C:\users\sjmac\src\ImageActionRunner\EXIF>exiftool -Comment= a.jpg
    1 image files updated

C:\EXIF>.\exiftool.exe -Comment a.jpg

C:\EXIF>.\exiftool.exe -Title="New Title" "-Comment<${Comment;$_='New Comment'}" a.jpg
Warning: [minor] Tag 'Comment' not defined - a.jpg
Warning: No writable tags set from a.jpg
    1 image files updated

C:\EXIF>.\exiftool.exe -Comment a.jpg

...[no output! :) ]...

C:\EXIF>.\exiftool.exe -Comment="Wrong Value" a.jpg
    1 image files updated

C:\EXIF>.\exiftool.exe -Comment a.jpg
Comment                         : Wrong Value

C:\EXIF>.\exiftool.exe -Title="New Title" "-Comment<${Comment;$_='New Comment'}" a.jpg
    1 image files updated

C:\EXIF>.\exiftool.exe -Comment a.jpg
Comment                         : New Comment



I'll have to leave diagnosing the powershell problem for another day, but now I'm working. Thanks :)

Phil Harvey

I thought you wanted to write Comment only if it previously existed.  So in your latest example nothing was written to Comment because it didn't exist.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

busywait

Yes, I've updated the example to show that works correctly too

My new value is written to Comment only if Comment has some value already - the output above shows that tested both ways.