Setting Date from filename

Started by osamarao, February 28, 2025, 05:57:00 PM

Previous topic - Next topic

osamarao

I apologize if this has been asked. I have searched but my specific problem hasn't been asked.

I have a file that I name as DSCF3325_02_22_2025_20_42_51.avif

Here the format is FILENAMEVIACAM_Month_day_year_hour_minute_second.avif

I have tried this command

exiftool -v -d "%m_%d_%Y_%H_%M_%S" '-CreateDate<${FileName;s/^[^_]*_//; s/\.[^.]*$}' DSCF3325_02_22_2025_20_42_51.avif

This runs, but ends up using the last 4 in the file name as the year. i.e.

> exiftool -CreateDate DSCF3325_02_22_2025_20_42_51.avif
> Create Date                     : 3325:02:22 20:25:20


I have used echo and sed to make sure only the date portion is selected via the string selector, the standard out shows only the date portion, so I'm not sure what I am missing here.

P.S. As an aside, What is the specific name of this selector, the one that uses s/// format)

StarGeek

Quote from: osamarao on February 28, 2025, 05:57:00 PMI apologize if this has been asked. I have searched but my specific problem hasn't been asked.

It has, many times (for example). The problem is that you need to recognize that the year/month/day is out of order and search on that.

The first thing I would suggest is to make sure you actually need to do this, because it seems very unlikely that an AVIF file would need to be edited. Run this command on the file to see all the time stamps in the file
exiftool -time:all -G -a -s file.avif

You should see output that look similar to this
[File]          FileModifyDate                  : 2025:01:19 07:40:54-08:00
[File]          FileAccessDate                  : 2025:02:28 15:17:02-08:00
[File]          FileCreateDate                  : 2024:11:23 07:49:15-08:00
[EXIF]          ModifyDate                      : 2025:02:28 12:00:00
[EXIF]          DateTimeOriginal                : 2025:02:28 12:00:00
[EXIF]          CreateDate                      : 2025:02:28 12:00:00

If those three EXIF time stamps properly match the filename, then you don't need to do anything. The data is already there.

The reason I ask this is because I suspect you are looking at the files with a program that doesn't properly read the embedded metadata and that makes you think you need to fix this. AVIF are relatively new and a lot of programs have yet to catch up with reading the metadata of such files.

If you do need to fix things, I would suggest
exiftool '-AllDates<${Filename;m/(\d\d)_(\d\d)_(\d{4})(.*)/$_=$3.$2.$1.$4;}' /path/to/files/

You didn't mention your OS, but I'm assuming Mac/Linux because of the single quotes. If you're on Windows, change the single quotes to double quotes and use CMD, not Powershell.

This command will extract all the date/time numbers, reorder them, and then use that as the filename. The filename is then copied into the AllDates shortcut. See FAQ #5, How do I format date and time information for writing, the paragraph starting "Having said this".

The AllDates is a shortcut for the three most common EXIF timestamps, CreateDate, DateTimeOriginal, and ModifyDate and will write all three at once.

QuoteP.S. As an aside, What is the specific name of this selector, the one that uses s/// format)

That is the Perl substitution operator. In the command I listed, I used the Perl match operator. Exiftool is a Perl program so a lot of the more technical commands are basically Perl code embedded in the command.
"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

osamarao

Thank you. Again, apologies, but the search engines weren't getting specific enough.

As with regards to AVIF info, I'm using darktable to export to HEIC, where the container is then AVIF. Darktable however is not writing the CreatedDate tag and all the dates are set to date of export/modification.

Now here's the command line while running the command you posted.
➜  darktable_exported exiftool -time:all -G -a -s DSCF3325_02_22_2025_20_42_51.avif
[File]          FileModifyDate                  : 2025:02:28 17:54:23-06:00
[File]          FileAccessDate                  : 2025:02:28 17:54:26-06:00
[File]          FileInodeChangeDate             : 2025:02:28 17:54:23-06:00
[ICC_Profile]   ProfileDateTime                 : 2025:02:28 18:14:48
➜  darktable_exported exiftool '-CreateDate<${Filename;m/(\d\d)_(\d\d)_(\d{4})(.*)/$_=$3.$2.$1.$4;}' .
Warning: syntax error for 'Filename' - ./DSCF3325_02_22_2025_20_42_51.avif
    1 directories scanned
    1 image files updated
➜  darktable_exported exiftool -time:all -G -a -s DSCF3325_02_22_2025_20_42_51.avif                   
[File]          FileModifyDate                  : 2025:02:28 17:54:52-06:00
[File]          FileAccessDate                  : 2025:02:28 17:54:55-06:00
[File]          FileInodeChangeDate             : 2025:02:28 17:54:52-06:00
[EXIF]          CreateDate                      : 3325:02:22 20:25:20
[ICC_Profile]   ProfileDateTime                 : 2025:02:28 18:14:48

It does not seem to have worked.


Also, I kept working on my version of the command, its odd that matching one or more (+) instead of zero or more (*), worked. Why do you think that is? Some quirk of Perl?

The command that works looks like this :

exiftool -v -d "%m_%d_%Y_%H_%M_%S" '-CreateDate<${FileName;s/^[^_]+_//; s/\.[^.]+$//}' DSCF3325_02_22_2025_20_42_51.avif

StarGeek

Sorry, it's a mistake I'm making more often for some reason. I forgot the semicolon. I also swapped the dates incorrectly.
${Filename;m/(\d\d)_(\d\d)_(\d{4})(.*)/;$_=$3.$1.$2.$4;}

C:\>exiftool -P -overwrite_original "-AllDates<${Filename;m/(\d\d)_(\d\d)_(\d{4})(.*)/;$_=$3.$1.$2.$4;}" Y:\!temp\x\y\DSCF3325_02_22_2025_20_42_51.jpg
    1 image files updated

C:\>exiftool -time:all --system:all -G -a -s Y:\!temp\x\y\DSCF3325_02_22_2025_20_42_51.jpg
[EXIF]          ModifyDate                      : 2025:02:22 20:42:51
[EXIF]          DateTimeOriginal                : 2025:02:22 20:42:51
[EXIF]          CreateDate                      : 2025:02:22 20:42:51
"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

osamarao

Thank you.

Any ideas on the second part of my last reply?

Phil Harvey

Quote from: osamarao on February 28, 2025, 06:59:52 PMits odd that matching one or more (+) instead of zero or more (*), worked. Why do you think that is? Some quirk of Perl?

Likely some other syntax error somewhere.  Either should work fine.

- 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

Quote from: osamarao on February 28, 2025, 06:59:52 PMAlso, I kept working on my version of the command, its odd that matching one or more (+) instead of zero or more (*), worked. Why do you think that is? Some quirk of Perl?

Works correctly here
C:\Programs\My_Stuff>exiftool -P -overwrite_original -d "%m_%d_%Y_%H_%M_%S" "-CreateDate<${FileName;s/^[^_]*_//; s/\.[^.]*$//}" Y:\!temp\x\y\DSCF3325_02_22_2025_20_42_51.jpg
    1 image files updated

C:\Programs\My_Stuff>exiftool -time:all --system:all -G1 -a -s Y:\!temp\x\y\DSCF3325_02_22_2025_20_42_51.jpg
[ExifIFD]       CreateDate                      : 2025:02:22 20:42:51l

Though I do see a problem if you were using substitutions (or matches) like this in an -if option. See Note #3 under that option. The $/ will cause an if to fail. I tend to place the dollar sign in a noncapture group (?:$)/ because that's more like proper RegEx than $$ would be.
"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

osamarao

Yep, its a syntax error. Gist here

I realized this after using this in the command line.

> darktable_exported echo DSCF3325_02_22_2025_20_42_51.avif | sed 's/^[^_]*_//' | sed 's/\.[^.]*$' 
sed: 1: "s/\.[^.]*$": unterminated substitute pattern

Its probably out of scope for exiftool, but it would be great if the tool to could output syntax errors too. i.e. the one sed showed


Thank you for all your help

StarGeek

You mean like this?
C:\>exiftool -P -overwrite_original -d "%m_%d_%Y_%H_%M_%S" "-CreateDate<${FileName;s/^[^_]*_//; s/\.[^.]*$}" Y:\!temp\x\y\DSCF3325_02_22_2025_20_42_51.jpg
Warning: Substitution pattern not terminated for 'FileName' - Y:/!temp/x/y/DSCF3325_02_22_2025_20_42_51.jpg
Warning: No writable tags set from Y:/!temp/x/y/DSCF3325_02_22_2025_20_42_51.jpg
    0 image files updated
    1 image files unchanged

We can only help you with exiftool on the command line, not what Darktable does.
"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

osamarao

Thank you.

I must have been tired and not reading it fully. Thank you again.