ExifTool Forum

ExifTool => ExifTool GUI => Topic started by: rewallac on July 28, 2017, 10:04:20 AM

Title: Set Date Taken from File Name
Post by: rewallac on July 28, 2017, 10:04:20 AM
I have a folder of photos which have the date taken in the file name. How to I set the EXIF date taken from the file name?

2016-10-06_17-59-46.295790.jpg
2016-10-10_17-28-47.765140.jpg
2016-10-17_17-53-49.384150.jpg

YYYY-MM-DD_??-??-??.??????.jpg (Logically it would seem that the format is YYYY-MM-DD_HH-mm however, based on the content the hour/minute does not appear to me to be in the filename. However, I will accept those values as the hour/minute if I have to.
Title: Re: Set Date Taken from File Name
Post by: Phil Harvey on July 28, 2017, 10:13:12 AM
If you do this:

exiftool "-datetimeoriginal<filename" DIR

then the year, month, day, hour, minute, second will be set from the file name.  (See FAQ 5 (https://exiftool.org/faq.html#Q5) for why this works.)

If you want to set the minutes and seconds to zero, you can so this:

exiftool "-datetimeoriginal<${filename;$_=substr($_,0,13)} 00:00" DIR

This is assuming you want to set EXIF DateTimeOriginal.  You could also set CreateDate and/or ModifyDate if you want, or use AllDates (a shortcut tag (https://exiftool.org/TagNames/Shortcuts.html)) to set all three together.

- Phil
Title: Re: Set Date Taken from File Name
Post by: rewallac on July 28, 2017, 11:53:47 AM
PERFECT!

THANKS PHIL!
Title: Re: Set Date Taken from File Name
Post by: Azertix on April 09, 2020, 01:02:55 PM
Hello and thanks for your tool 8)
I want to do the same thing but my files are named "IMG-20190907-WA0025" (Whatsapp files).
Is it possible ?

Thank you ;D
Azertix
Title: Re: Set Date Taken from File Name
Post by: Phil Harvey on April 09, 2020, 01:10:12 PM
Hi Azertix,

That same command should work for you.

- Phil
Title: Re: Set Date Taken from File Name
Post by: Azertix on April 09, 2020, 02:01:53 PM
Correct, but not for all files, i don't know why  :-\
C:\Windows>exiftool "-datetimeoriginal<filename" C:\Temp\Images_a_traiter
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20190908-WA0069.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20190908-WA0069.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20190908-WA0072 - Copie.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20190908-WA0072 - Copie.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20190908-WA0072.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20190908-WA0072.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20190911-WA0074.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20190911-WA0074.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20191008-WA0070.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20191008-WA0070.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20191008-WA0071.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20191008-WA0071.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20191018-WA0061.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20191018-WA0061.jpg
Warning: Invalid date/time (use YYYY:mm:dd HH:MM:SS[.ss][+/-HH:MM|Z]) in ExifIFD:DateTimeOriginal (PrintConvInv) - C:/Temp/Images_a_traiter/IMG-20191028-WA0067.jpg
Warning: No writable tags set from C:/Temp/Images_a_traiter/IMG-20191028-WA0067.jpg
    1 directories scanned
   62 image files updated
    8 image files unchanged

I did those 8 files manually  :P
Title: Re: Set Date Taken from File Name
Post by: StarGeek on April 09, 2020, 02:42:31 PM
The second command would probably have been better for those you list, as it only takes the first group of numbers.  If all the images included the WA#### format, then some of them probably have hours and minutes also set instead of 00:00:00.
Title: Re: Set Date Taken from File Name
Post by: Phil Harvey on April 09, 2020, 03:01:14 PM
Yes I was looking at the second command
Title: Re: Set Date Taken from File Name
Post by: Azertix on April 10, 2020, 08:04:08 AM
Thanks for your replies ;D
Title: Re: Set Date Taken from File Name
Post by: gheppell on April 18, 2020, 01:35:02 AM
Hello.  I name many of my photos in the following fashion - 'May 10, 2004@13.22.58.jpg'    (without the quotes).  Is there any way to use exiftool to set a given date tag based on the filename as I have described it?  I suspect I will have to parse the filename piece by piece and attach the appropriate text parsed to the appropriate component of the date in the tag.  I can't seem to find the protocol by which I do this however.  Thank you
Title: Re: Set Date Taken from File Name
Post by: StarGeek on April 18, 2020, 03:02:10 AM
Try this
exiftool -d "%B %d, %Y@%H.%M.%S" "-DateTimeOriginal<${Filename;s/\.[^.]*$//}" <FileOrDir>

The -d (dateFormat) option (https://exiftool.org/exiftool_pod.html#d-FMT--dateFormat) tells exiftool how to interpret the filename as a date (see Common Date Format Codes (https://exiftool.org/filename.html#codes)).  The ${Filename;s/\.[^.]*$//} strips away the extension so it doesn't have to be dealt with in the date string.
Title: Re: Set Date Taken from File Name
Post by: gheppell on April 18, 2020, 12:24:07 PM
Thanks very much.  Depending on when I named/renamed the photo, there may or may not be a space after the comma, may or may not be a space before or after the ampersand, etc.  Will the command you suggest allow for these minor differences?  If not, what would a command look like that assumes the following:  1) first three alpha characters in the filename represent Month, 2) the first two numeric characters represent day, 3) the next four numeric characters represent year, and so on for the hours, minutes, seconds.  The order of these items is perfectly consistent in my filenames, just not some of the spacing and use of commas as I detailed above. 

As always, many thanks.

Also, just realized I used May in my example, which is unfortunate in that it happens to only have 3 letters.  For the month, I only use the three character abbreviation. (ie.  Jan for January, Feb, for February.....)


Another follow up:  I set one file to it's full month name and ran the tool as a batch file with the following command:

exiftool -d "%B %d, %Y@%H.%M.%S" "-xmp:createdate<${Filename;s/\.[^.]*$//}" -overwrite_original  "January 31, 2009@13.16.48.JPG"
pause

The output I received back (file path altered) was:

G:\{myfilepath}\Jan>exiftool -d "d, H.S" "-xmp:createdate<${Filename;s/\.[^.]*$//}" -overwrite_original  "January 31, 2009@13.16.48.JPG"
Warning: No writable tags set from January 31, 2009@13.16.48.JPG
    0 image files updated
    1 image files unchanged

G:\{myfilepath}\Jan>pause
Press any key to continue . . .

Regards.
Title: Re: Set Date Taken from File Name
Post by: StarGeek on April 18, 2020, 01:18:30 PM
Quote from: gheppell on April 18, 2020, 12:24:07 PM
may or may not be a space before or after the ampersand, etc.

Ampersand?  Do you mean the At Symbol @, as there isn't an Ampersand & in your example. 

QuoteWill the command you suggest allow for these minor differences?

It will allow for minor spacing differences but not if there are spaces where there aren't spaces in the date format string such as around the @ symbol.

Quote1) first three alpha characters in the filename represent Month,

I had assumed from your example, that the full month name was used.  If the month name is abbreviated, then use %b instead.  See Common Date Format Codes (https://exiftool.org/filename.html#codes).

It's probably easier to tell exiftool to remove all the spaces for this command.  Use this for the filename instead
${Filename;s/\.[^.]*$//;s/ //g}

Title: Re: Set Date Taken from File Name
Post by: Phil Harvey on April 18, 2020, 01:28:18 PM
StarGeek posted while I was composing this, but here it is anyway...

You should use %b instead of %B for an abbreviate month name.

In a .bat file you must double all "%" characters.  So try this in your .bat file:

exiftool -d "%%B %%d, %%Y@%%H.%%M.%%S" "-xmp:createdate<${Filename;s/\.[^.]*$//;s/^(.{3}).*?(\d+).*?(\d+).*?(\d+).*?(\d+).*?(\d+)/$1 $2, $3\@$4.$5.$6/}" -overwrite_original  %*

Here I have tried to also reformat the date/time to match the -d formatting.

- Phil
Title: Re: Set Date Taken from File Name
Post by: gheppell on April 18, 2020, 01:31:55 PM
Thank you both very much.  StarGeek, yes I meant the @ symbol, not the ampersand.  My apologies for any confusion.
Title: Re: Set Date Taken from File Name
Post by: mbrijun on July 11, 2020, 02:47:53 AM
Hi, this is a very useful functionality I have been looking for. One additional question - can exiftool detect if "AllDates" metadata is already present and skip that file without modifying it? Thank you.
Title: Re: Set Date Taken from File Name
Post by: StarGeek on July 11, 2020, 03:11:50 AM
I wouldn't know how to do it in the GUI (which is no longer supported (https://exiftool.org/forum/index.php?topic=4553.0)) but on the command line you can add the -wm (writemode) option (https://exiftool.org/exiftool_pod.html#wm-MODE--writeMode), specifically -wm cg which will create new tags and groups as necessary but will not write to existing tags.

But one thing to realize is that AllDates is a Shortcut tag (https://exiftool.org/TagNames/Shortcuts.html) and if one or two of the tags that are part of the shortcut don't exist, then it would write to those while leaving the others alone.  This could show up as discrepancies between time stamps.
Title: Re: Set Date Taken from File Name
Post by: mbrijun on July 11, 2020, 05:37:21 AM
Quote from: StarGeek on July 11, 2020, 03:11:50 AM
on the command line you can add the -wm (writemode) option (https://exiftool.org/exiftool_pod.html#wm-MODE--writeMode), specifically -wm cg which will create new tags and groups as necessary but will not write to existing tags.

this is exactly what I was looking for, thank you!
Title: Re: Set Date Taken from File Name
Post by: greglec on February 13, 2021, 10:21:59 AM
Hi all, first of all many thanks for the really useful app! Saved me from having thousands of photos badly dated.

I have been updating the dates from the file name very well for many but, on some photos, I am struggling with the filename parsing.

They are named as such: 'Photo on 02-01-2016 at 11.55 #3.jpg' or  'Photo on 02-01-2016 at 11.57.jpg'

I am trying the following command : 'exiftool -d "Photo on %d-%m-%Y at %H.%M" "-alldates<filename" DIR'

But I am getting the following errors :
Warning: garbage at end of string in strptime: .jpg in ExifIFD:DateTimeOriginal (PrintConvInv) - Photo_on/Photo on 17-11-2015 at 21.13.jpg
Warning: No writable tags set from Photo_on/Photo on 17-11-2015 at 21.13.jpg

I am assuming that I need to ignore the characters in the file name after the minutes with input such as '${Filename;s/\.[^.]*$//}' but I am a bit lost on how to use it.

Any help?

Many thanks in advance.
Title: Re: Set Date Taken from File Name
Post by: Phil Harvey on February 13, 2021, 11:50:56 AM
strptime is very picky about the syntax, but I think this should work:

exiftool -d "Photo on %d-%m-%Y at %H.%M.jpg" "-alldates<filename" DIR

If that doesn't work, I would resort to using an advanced formatting expression, something like this:

exiftool "-alldates<${filename;s/.*(\d{2})-(\d{2})-(\d{4}) at (\d{2})\.(\d{2}).*/$3:$2:$1 $4:$5:00/ or $_ = undef}" DIR

- PHil
Title: Re: Set Date Taken from File Name
Post by: Jeneeben on April 14, 2021, 02:56:53 AM
Hello,

I need to change the creation date of some scans based on the file name.
The syntax of the filename is: YYYY_MM_XXX and YYYY_MM

I tried to adjust the provided command: exiftool '-datetimeoriginal<${filename;$_=substr($_,0,7)} 00:00:00'

But I am getting "No writable tags set from ..." warning message.

How can I change the command to be able to get the Date from my file name?

Thanks in advance,
Jeneeben
Title: Re: Set Date Taken from File Name
Post by: StarGeek on April 14, 2021, 10:53:45 AM
A timestamp requires 14 numbers and if you give only the year and month, thats only 6 plus 6 more for the time portion.  You need to add a day to the command
Try
'-datetimeoriginal<${filename;$_=substr($_,0,7)} 01 00:00:00'
Title: Re: Set Date Taken from File Name
Post by: Jeneeben on April 14, 2021, 12:22:10 PM
Perfect, it is working :)

Thanks!
Title: Re: Set Date Taken from File Name
Post by: kyber on June 23, 2022, 06:37:27 PM
 :) Worked for me! issues were in datetimeoriginal vs created

thank you phil