Confused. Need to rename a file and simultaneously assign a value to a date tag

Started by arretx, July 28, 2022, 11:31:06 PM

Previous topic - Next topic

arretx

So, I'm at the end of my pay grade here as I've been thwarted mainly by programming syntax.

The name of my file(s) look like this:

BabyCam1 (0422) - 2021-04-01 - 05.00.00.MOV

Yes, all of those spaces and all of those dashes and dots. 

I don't understand regex yet, and I definitely know nothing about Perl, but what I've gleaned from my tinkering is that I can do this:

exiftool '-TestName<${filename;s/BabyCam1/Turd/}' <filename>
And I effectively substitute "BabyCam1" with the word "Turd" and the filename result in the test is as expected.

However, here is the actual result I need from the file:

BabyCam1 (0422) - 2021-04-01 - 05.00.00.MOV
which needs to become:

BabyCam1_0000001.MOV
When run on an entire folder tree, the 7 digit number should increment to prevent duplicates.

Most importantly, I need the part of the current file that has the date and the hour added to the new file's CreateDate tag like 2021:04:01 05:00:00 with an offset of -07:00 if that matters.

I don't know how to run a single exiftool command that can store a portion of the filename, reformat that portion, then update the CreateDate tag while simultaneously renaming the file, stripping all of the information but the 'BabyCam1' then adding a 7 digit serial number to the end while preserving the extension.

Help? 

StarGeek

The renaming part would be handled with something like this. Change Testname to Filename if the output looks correct to you.
'-TestName<${Filename;m/^([^\s]+) /;$_=$1}_%.7nC.%e'

This uses regex to match and capture all non-whitespace characters from the beginning of the filename until the first space (m/^([^\s]+) / example on Regex101).  The value of filename is then replaced with the captured value ($_=$1).  That takes care of the first part of the filename.

Then the %C variable is used to add the incremental count (see the Advanced features section of the -w (-TextOut) option).  The (dot)7 indicates a field width of 7 and the n indicates starting position of 1.

Now, copying the date from the Filename at the same time gets tricky, as well as the fact that it's a video instead of an image.  When you edit a file at the same time as renaming it, exiftool will create a copy of the file (see Notes on the Writing "FileName" and "Directory" tags page).  If you don't want to create a copy, you need to add the -overwrite_original option.

Video time stamps are supposed to be in UTC according to the specs.  Assuming the date time in the filename is in the local time, it will need to be adjusted.  If the computer is in the same time zone as where the video was shot, ignoring Daylight time differences, then you can add the -api QuickTimeUTC option and exiftool will automatically adjust. This part of the command would be along these lines
-api QuickTimeUTC '-CreateDate<${Filename;s/^.*? - /}'

If the computer is in a different time zone, then you have to add the time zone
-api QuickTimeUTC '-CreateDate<${Filename;s/^.*? - /}-07:00'

Putting it all together
exiftool -overwrite_original -api QuickTimeUTC '-Filename<${Filename;m/^([^\s]+) /;$_=$1}_%.7nC.%e' '-CreateDate<${Filename;s/^.*? - /}' /path/to/files/

If you want to make sure the FileModifyDate doesn't change, then you can add the -P (-preserve) option.
* 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).