Project

General

Profile

Date and time from filename to exif and file dates

Added by Anonymous over 5 years ago

Hi pro's!
Does anyone has a ready to use script or command line for import Date and time from filename to exif and file dates. I explain, there are files which names starts in format YYYYMMDD_HHMMSS... Like 20160606_231834... So, this date and time I need to set in EXIF, at least, and to modified/created date.
I wonder if some pro can write it so other use it.
Please.


Replies (5)

RE: Date and time from filename to exif and file dates - Added by Robin Mills over 5 years ago

The Exif format for DateTime is YYYY:MM:DD hh:mm:ss

575 rmills@rmillsmbp:~ $ exiv2 -pa --grep DateTime ~/Stonehenge.jpg 
Exif.Image.DateTime                          Ascii      20  2015:07:16 20:25:28
Exif.Photo.DateTimeOriginal                  Ascii      20  2015:07:16 15:38:54
Exif.Photo.DateTimeDigitized                 Ascii      20  2015:07:16 15:38:54
There are many ways to convert YYYYMMDD_HHMMSS to the Exif format. Here's one way in bash:
576 rmills@rmillsmbp:~ $ x=20160606_231834
577 rmills@rmillsmbp:~ $ echo ${x:0:4}:${x:4:2}:${x:6:2} ${x:9:2}:${x:11:2}:${x:13:2}
2016:06:06 23:18:34
578 rmills@rmillsmbp:~ $ 

RE: Date and time from filename to exif and file dates - Added by Anonymous over 5 years ago

sorry, I forgot to say that's for Windows 10. For files which are in same folder

RE: Date and time from filename to exif and file dates - Added by Robin Mills over 5 years ago

Ah. So bash isn't immediately available. Perhaps you're looking for the exiv2 -T option which updates the filestamp from the metadata.

617 rmills@rmillsmbp:~ $ curl --silent -O http://clanmills.com/Stonehenge.jpg
618 rmills@rmillsmbp:~ $ ls -alt Stonehenge.jpg 
-rw-r--r--  1 rmills  staff  6757827  6 Jun 23:05 Stonehenge.jpg
619 rmills@rmillsmbp:~ $ exiv2 -pa --grep DateTime Stonehenge.jpg 
Exif.Image.DateTime                          Ascii      20  2015:07:16 20:25:28
Exif.Photo.DateTimeOriginal                  Ascii      20  2015:07:16 15:38:54
Exif.Photo.DateTimeDigitized                 Ascii      20  2015:07:16 15:38:54
620 rmills@rmillsmbp:~ $ exiv2 -T Stonehenge.jpg 
621 rmills@rmillsmbp:~ $ ls -alt Stonehenge.jpg 
-rw-r--r--  1 rmills  staff  6757827 16 Jul  2015 Stonehenge.jpg
622 rmills@rmillsmbp:~ $ 
I don't really understand what you'd like to achieve, however I'm confident that together we can fix this.

RE: Date and time from filename to exif and file dates - Added by Robin Mills over 5 years ago

Here are the above bash/macosx tips converted to DOS/cmd.exe:

C:\temp>set x=20160606_231834
C:\temp>echo %x:~0,4%:%x:~4,2%:%x:~6,2% %x:~9,2%:%x:~11,2%:%x:~13,2%
2016:06:06 23:18:34

C:\temp>

curl is a utility to download a file from the internet. exiv2 -T updates the file timestamp using Exif.Photo.DateTimeOriginal
C:\temp>curl --silent -O http://clanmills.com/Stonehenge.jpg

C:\temp>dir *.jpg
 Volume in drive C has no label.
 Volume Serial Number is 0899-EF40

 Directory of C:\temp

06/07/2016  07:53 AM         6,757,827 Stonehenge.jpg
               1 File(s)      6,757,827 bytes
               2 Dir(s)  27,387,289,600 bytes free

c:\temp>exiv2 -pa --grep DateTime Stonehenge.jpg
Exif.Image.DateTime                          Ascii      20  2015:07:16 20:25:28
Exif.Photo.DateTimeOriginal                  Ascii      20  2015:07:16 15:38:54
Exif.Photo.DateTimeDigitized                 Ascii      20  2015:07:16 15:38:54
c:\temp>exiv2 -T Stonehenge.jpg

c:\temp>dir *.jpg
 Volume in drive C has no label.
 Volume Serial Number is 0899-EF40

 Directory of c:\temp

07/16/2015  03:38 PM         6,757,827 Stonehenge.jpg
               1 File(s)      6,757,827 bytes
               0 Dir(s)  27,228,958,720 bytes free

c:\temp>

RE: Date and time from filename to exif and file dates - Added by Robin Mills over 5 years ago

Here's the whole thing collected together:

1 Set up a file with the name YYYYMMDD hhmmss.jpg

c:\temp>move Stonehenge.jpg "20160102 030405.jpg" 
        1 file(s) moved.

c:\temp>dir *.jpg
 Volume in drive C has no label.
 Volume Serial Number is 0899-EF40

 Directory of c:\temp

07/16/2015  03:38 PM         6,757,827 20160102 030405.jpg
               1 File(s)      6,757,827 bytes
               0 Dir(s)  27,198,996,480 bytes free

2 Run over the directory and update the metadata from the filename

c:\temp>for %x in (*.jpg) do exiv2 -M"set Exif.Photo.DateTimeOriginal %x:~0,4%:%x:~4,2%:%x:~6,2% %x:~9,2%:%x:~11,2%:%x:~13,2%" "%x" 
exiv2 -M"set Exif.Photo.DateTimeOriginal 2016:01:02 03:04:05" "20160102 030405.jpg" 

c:\temp>

3 Update the timestamp of the file using the metadata

c:\temp>exiv2 -T "20160102 030405.jpg" 

c:\temp>dir *.jpg
 Volume in drive C has no label.
 Volume Serial Number is 0899-EF40

 Directory of c:\temp

01/02/2016  03:04 AM         6,778,265 20160102 030405.jpg
               1 File(s)      6,778,265 bytes
               0 Dir(s)  27,198,812,160 bytes free

c:\temp>
    (1-5/5)