1

There is a set of photos with timestamps in their filenames like these:

Photo on 3-09-12 at 9.24 PM #2.jpg

Photo on 3-09-12 at 9.24 PM #1.jpg

Photo on 3-09-12 at 8.23 PM.jpg

etc.

("3-09-12", means "3rd Sep 2012" or DD-MM-YY)

But these photos have no EXIF data at all. Before you imported them to a larger collection, how would you pipe this information to exiftool and also tell it to add new timestamps as EXIF data, all from the photos' filenames?

Update: (The now Pt. 1 of) my question about parsing filenames with DD-MM-YY and 12 hour time has been very kindly answered by @Stephane. But I discovered that the same batch of photos contains filenames with one variation I had missed. I hope it makes more sense to add a 'Pt. 2' instead of starting a whole new question.

In short:

How could I change @Stephane's brilliant answer --

exiftool '-CreateDate<${FileName;use Date::Manip;
                      Date_Init("DateFormat=non-US");
                      /on (.*?at.*?[AP]M)/;$_=$1;
                      y/./:/;$_=UnixDate($_,"%Y-%m-%d %T")
                      }' ./*on\ *at*[PA]M*.jpg

-- so that it might work with filename format below, which is slightly different from the first part of the question, in that it uses YYYY-MM-DD and 24-hour time:

Photo on 2010-09-15 at 18.44 #4.jpg

Photo on 2010-09-15 at 18.44 #3.jpg

Photo on 2010-09-15 at 18.44.jpg

More explanation:

Trying to edit the Date::Manip part of the Stephane's script seems to show up my ignorance of what's going on in the most important parts of it.

I tried omitting the Date_Init line since we are back to an ISO-esque full-year date format and then having

/on (.*?at.*?)/;$_=$1;
y/./:/;$_=UnixDate($_,"%Y-%m-%d %T")
' ./*on\ *at*.jpg

But exiftool is giving me no writeable tags and FileName not defined. Instructions at http://search.cpan.org/~sbeck/Date-Manip-5.56/lib/Date/Manip.pod don't seem to be helpful (at least to me) in understanding what's going on with those periods, that 'y' at the start of the line, semicolon etc, and they're rather ungoogleable :S

2

Try:

exiftool '-CreateDate<${FileName;use Date::Manip;
                      Date_Init("DateFormat=non-US");
                      /on (.*at.*?)(?: #\d+)?\.jpg$/;$_=$1;
                      y/./:/;$_=UnixDate($_,"%Y-%m-%d %T")
                      }' ./*on\ *at*.jpg

(you may have to install the Date::Manip perl module).

The -Tag<value sets the corresponding tag. The ${tag;perl-code} can be used to expand to the value of tag after it has been processed by the perl-code.

Here, the plan is to use Date::Manip's UnixDate function to parse the date in the filename and convert it to a format acceptable for the CreateDate tag (2011-04-15 21:38:00).

Date::Manip understands a lot of common date formats. For instance, it understands 3-09-12 at 9:24 PM (though you have to tell it whether it's the US or non-US convention where the day or month is first) and 2010-09-15 at 18.44 (note the : instead of .).

So what we do is extract that part from the filename, convert the . to : and pass it to UnixDate.

  • 1
    For any interested Mac users, I fixed ExifTool's not seeing p5.8-datemanip by getting rid of the generic version from ExifTool's homepage and replacing it with MacPorts' version. Remove it: sudo rm -rf /usr/bin/exiftool /usr/bin/lib. Then do sudo port install p5.8-image-exiftool. Obviously, you also need to have MacPorts' p5.8-datemanip and perl5.8. Then new ExifTool executable is exiftool-5.8. – iceequal May 9 '14 at 8:55
  • Stephane, just realised that editing your script to accept the format "3-09-12" as "3rd Sep 2012" is going to be rather complicated. Do you know which part of the Date::Manip manual I should be looking at, or would you know offhand how to change the script appropriately? :) – iceequal May 9 '14 at 9:23
  • @iceequal, see edit. Note that if using MacPorts, I suppose you don't have to use such an antique perl version, exiftool works well with 5.18. – Stéphane Chazelas May 9 '14 at 13:52
  • Oof, just discovered another (separate) batch of photos created by the same program but with, for some reason, a slightly different format: Photo on 2011-04-15 at 21.38 #3.jpg and Photo on 2010-09-15 at 18.42 etc. Would you have any idea about those? Or is there a good link that might explain some of Date::Manip’s functions for beginners? – iceequal May 13 '14 at 8:04
  • @iceequal Date::Manip will handle those out of the box. You just need to adapt the regexp that extracts the date+time from the filename. See the edit. – Stéphane Chazelas May 16 '14 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.