Rename digital photo files according to EXIF date information
Please note that while I provide this script in the hopes that it will be useful to you, I make NO GUARANTEES OR WARRANTIES OF ANY KIND. The code is provided subject to the same terms as Perl itself. For more information, please refer to the Perl Artistic License.
#!/usr/bin/perl
# Image files will renamed to YYYY_MMDD_HHMMSSxx where xx is
# a two letter suffix to distinguish images that were taken within
# the same second
use strict; use warnings;
use File::Slurp;
use Image::ExifTool 'ImageInfo';
my @files = grep { /.jpe?g\z/i } read_dir;
rename_to_exif_date($_) for (@files);
sub rename_to_exif_date {
my ($img) = @_;
my $ts = ImageInfo($img, 'DateTimeOriginal')->{DateTimeOriginal};
return unless defined $ts;
my $d4 = '([0-9]{4})';
my $d2 = '([0-9]{2})';
if( my ($year, $mon, $day, $hr, $min, $sec) = (
$ts =~ /^$d4 : $d2 : $d2 \s $d2 : $d2 : $d2 \z/ ) ) {
my $new_name = "${year}_$mon${day}_$hr$min$sec";
my $suffix = 'aa';
++$suffix while -e "$new_name$suffix.jpg";
$new_name .= "$suffix.jpg";
return rename $img, $new_name;
}
return;
}__END__