#!/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 warning? print "Warn: Processing '$file': " . $exifTool->GetValue('Warning') . "\n"; } # I suppose we could still check to make sure the data is "reasonable". # 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