ExifTool Forum

ExifTool => The "exiftool" Application => Topic started by: Magnus_CA on June 29, 2016, 01:58:20 PM

Title: How to rename file with tag if it exists, otherwise use set value
Post by: Magnus_CA on June 29, 2016, 01:58:20 PM
Hi,

Here's the command I'm using to rename my files (from windows cmd line):

exiftool -config .ExifTool_config -v -ext jpg "-Filename<${datetimeoriginal}${SubSecTimeOriginal}%+3c.%le" -d "IMG_%Y%m%d_%H%M%S"  [i]DIR[/i]

This works perfectly if SubSecTimeOrignal exists but I'd like it to write a '00' in the instances it doesn't exist and append a sequence number if there's a file name collision.  I'm not sure how to conditionally apply this and I'd prefer to be able to rename all my files in a single pass.

Thanks in advance for the replies.

Jason
Title: Re: How to rename file with tag if it exists, otherwise use set value
Post by: Phil Harvey on June 29, 2016, 02:01:17 PM
Hi Jason,

You can assign multiple values to any tag on the command line.  The last valid assignment takes precedence.  So the command looks something like this:

exiftool -TAG="default value" "-TAG<OTHERTAG1" "-TAG<OTHERTAG2" ...

TAG will take the value of OTHERTAG2 if it exists, otherwise OTHERTAG1, and finally "default value" if neither exists.

- Phil
Title: Re: How to rename file with tag if it exists, otherwise use set value
Post by: Magnus_CA on June 29, 2016, 03:33:53 PM
Thanks!  So would those tag assignments exist as local variables that are only defined for the sake of the filename command that executes in the same command?  I'd rather not write any metadata just to satisfy the rename operation.

Would this be the command in my application?

exiftool -v -Filename<${datetimeoriginal}${SubSecTimeOriginal}%+3c.%le" -d "IMG_%Y%m%d_%H%M%S" -SubSecOriginal=00 -SubSecOriginal=${SubSecTimeOriginal}  DIR -ext jpg
Title: Re: How to rename file with tag if it exists, otherwise use set value
Post by: Phil Harvey on June 29, 2016, 03:39:09 PM
The answer to the question is "no", although I don't think you understood what I was trying to tell you.

To add a default SubSecTimeOriginal, you would do something like this:

exiftool "-Filename<${datetimeoriginal}00%+3c.%le"" "-Filename<${datetimeoriginal}${SubSecTimeOriginal}%+3c.%le" ...

- Phil
Title: Re: How to rename file with tag if it exists, otherwise use set value
Post by: Magnus_CA on June 30, 2016, 01:37:08 PM
Quote from: Phil Harvey on June 29, 2016, 02:01:17 PM

You can assign multiple values to any tag on the command line.  The last valid assignment takes precedence.  So the command looks something like this:


Phil,

This was eye opening.  Thank you very much.  I am very glad to have received your reply.

Cheers!
Title: Re: How to rename file with tag if it exists, otherwise use set value
Post by: llucax on September 20, 2020, 09:43:28 AM
Hi, I'm trying this trick to do some renaming and it doesn't seem to be working for me. The story is I have some mp4 files that seem to have no metadata associated for `DateTimeOriginal` and `model` tags, and in that case I want to just use the `FileModifyDate` and original `FileName` instead:


$ exiftool -verbose -fixBase -preserve  -ignoreMinorErrors -dateFormat '%Y%m%d_%H%M%S' -'FileName<$FileModifyDate-$FileName' -'FileName<$DateTimeOriginal-$model%-c.%le'   -- *.mp4
======== 20200920_130854-20200920_130854--.mp4
Setting new values from 20200920_130854-20200920_130854--.mp4
'20200920_130854-20200920_130854--.mp4' --> '-.mp4'
    1 image files updated


As you see the -'FileName<$DateTimeOriginal-$model%-c.%le' seems to be picked up even when the tags are missing. Also in this case the mp4 have other similar tags defined, like:

Media Create Date               : 2019:12:31 23:10:37
Author                          : LG-H870/


That I could use. Ideally I would like to be able to pick a certain tag, and if that one is not set another one and so on. For example, with a syntax similar to sh: ${DateTimeOriginal:-${MediaCreateDate:-$FileModifyDate};}-${model:-${Author:-};}.%le

Is there a way to do something like this instead of defining all the possible combination of existing tags in a series of -TAG -TAG -TAG?

Thanks!
Title: Re: How to rename file with tag if it exists, otherwise use set value
Post by: StarGeek on September 20, 2020, 11:25:55 AM
From the docs on -m (ignoreMinorErrors) option (https://exiftool.org/exiftool_pod.html#m--ignoreMinorErrors) (emphisis mine)
   Note that this causes missing values in -tagsFromFile, -p and -if strings to be set to an empty string rather than an undefined value.

Because of the -m option, DateTimeOriginal and Model will always have a value (a 0 length empty string) and the first rename will never take place.  Remove the -m and you should get the desired results.