Blaufish’s Image Rename Script – revisited

adopted to my new camera, performance optimized, file overwrite safety, dynamic selection of serial number variant, CR2 file support…


# Blaufish’s Image Rename Script
# https://blaufish.wordpress.com/
#
# Some rights reserved;
# http://creativecommons.org/licenses/by-nc-sa/3.0/
#
use Image::ExifTool qw( : Public);
use File::Copy;

my $destination = $ARGV[0];
my $exifTool = new Image::ExifTool;
$exifTool->Options(DateFormat=>"%Y-%m-%d %H%M%S");

sub renameImages {
 my($regexp,$extension) = @_;

 opendir(DIR, ".");
 my @files = grep(/$regexp/,readdir(DIR));
 closedir(DIR);
 foreach my $file (@files) {
   my $info = $exifTool->ImageInfo($file);
   #foreach (keys %$info) {  print "$_ => $$info{$_}\n"; }

   my $date = $info->{'DateTimeOriginal'};
   my $camera = $info->{'Model'};
   my $serial1 = $info->{'SerialNumber'};
   my $serial2 = $info->{'InternalSerialNumber'};
   my $fileno = $info->{'FileNumber'};

   my $newfile = $date . " " . $camera . " ";
   if ($serial1) { $newfile .= $serial1 . " "; }
   if ($serial2) { $newfile .= $serial2 . " "; }
   $newfile .= $fileno . "." . $extension ;

   if ($fileno eq "") {
      print "$file -- no fileno, ignore!\n";
      next;
   }

   if ($newfile eq $file) {
      print "$newfile -- allready has correct file name.\n";
      next;
   }

   if (-e $newfile) {
      print "$newfile -- exists! will not overwrite!\n";
      next;
   }

   print $newfile . "\n";
   rename($file, $newfile) or die "File cannot be renamed to $newfile.";
 }
}

renameImages("[0-9]+\\.(jpg|JPG)\$","jpg");
renameImages("[0-9]+\\.(cr2|CR2)\$","cr2");
#renameImages("[0-9]+\\.(mov|MOV)\$","mov");
Annonser

Kommentera

Fyll i dina uppgifter nedan eller klicka på en ikon för att logga in:

WordPress.com Logo

Du kommenterar med ditt WordPress.com-konto. Logga ut / Ändra )

Twitter-bild

Du kommenterar med ditt Twitter-konto. Logga ut / Ändra )

Facebook-foto

Du kommenterar med ditt Facebook-konto. Logga ut / Ändra )

Google+ photo

Du kommenterar med ditt Google+-konto. Logga ut / Ändra )

Ansluter till %s