I catalog images by the date each was taken. My camera timestamps its images with the date/time. Perfect!
However, when I later "modify" an image, or when I transfer it over BlueTooth, etc. the "Date Modified" for the file changes.
I use this little script to change the atime/mtime for the image file BACK to where it should be.
All the action is inside Image::ExifTool, nevertheless, I welcome your feedback!
UPDATE: Added error checking.
#!/usr/bin/perl
use warnings;
use strict;
use Time::Local;
use Image::ExifTool qw(:Public);
my $exifTool = new Image::ExifTool;
foreach my $file (@ARGV) {
# Read only the "DateTimeOriginal" tag from the file.
my $info = $exifTool->ImageInfo($file, 'DateTimeOriginal');
# Error Handling
if (defined $exifTool->GetValue('Error')) {
print "ERROR: Skipping '$file': " . $exifTool->GetValue('Error') .
+ "\n";
next;
}
if (not exists $info->{'DateTimeOriginal'}) {
warn "File '$file' has no EXIF 'DateTimeOriginal' tag. Skipping it
+.\n";
next;
}
if (defined $exifTool->GetValue('Warning')) { # Can there be a warn
+ing?
print "Warn: Processing '$file': " . $exifTool->GetValue('Warning'
+) . "\n";
}
# I suppose we could still check to make sure the data is "reasonabl
+e".
# There could be garbage data in the DateTimeOriginal field and we'd
+ be
# in trouble...
# Our data comes in the form "YEAR:MON:DAY HOUR:MIN:SEC".
# utime() wants data in the exact opposite order, so we reverse().
my @date = reverse(split(/[: ]/, $info->{'DateTimeOriginal'}));
# Note that the month numbers must be shifted: Jan = 0, Feb = 1
--$date[4];
# Convert to epoch time format
my $time = timelocal(@date);
# Make the change to the mtime and atime
my $status = utime($time, $time, $file);
if ($status != 1) {
print "Warn: utime() on '$file' returned $status instead of 1.\n";
}
}
__END__
=head1 SYNOPSYS
Reads the "DateTimeOriginal" EXIF field out of an image file
and changes the "last accessed time" and "last modified time"
of the file to match it.
=head1 USAGE
On Unix:
thisfile.pl *.jpg another/dir/*.jpg
On Windows:
perl thisfile.pl bunch.jpg of.jpg files.jpg
Everything you give to this file is expected to be a filename.
It accepts no other switches or arguments.
=cut