How to rename file with tag if it exists, otherwise use set value

Started by Magnus_CA, June 29, 2016, 01:58:20 PM

Previous topic - Next topic

Magnus_CA

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

Phil Harvey

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

Magnus_CA

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

Phil Harvey

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

Magnus_CA

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!

llucax

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!

StarGeek

From the docs on -m (ignoreMinorErrors) option (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.
* Did you read FAQ #3 and use the command listed there?
* Please use the Code button for exiftool code/output.
 
* Please include your OS, Exiftool version, and type of file you're processing (MP4, JPG, etc).