public
Created

  • Download Gist
rename.pl
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/usr/bin/env perl
use v5.10;
use utf8;
use strict;
use warnings;
use autodie;
use File::Copy;
use Cwd;
 
 
say("Application started.");
 
my $dir = cwd();
if (@ARGV == 1) {
$dir = shift (@ARGV);
}
say("Directory: $dir");
 
say("Opening directory...");
opendir(DIR, $dir) or die "can't open dir $dir: $!";
 
my $counter_renamed = 0;
my $counter_ignored = 0;
say("Beginning bulk rename...");
while (defined(my $file = readdir(DIR))) {
if($file =~ /\.(tga|png|jpe?g)/i){
my $new_file = $file;
$new_file =~ s/\d{1}_//i;
$new_file =~ s/([0-9]+)/sprintf('%06d',$1)/ige;
if ($file eq $new_file){
say ("Ignored file '$file' (New filename would not differ from current filename)");
$counter_ignored++;
next;
}
if (move("$dir/$file", "$dir/$new_file")){
$counter_renamed++;
say("Renamed file '$file' -> '$new_file'");
}
else{
say("Couldn't rename file '$file': $!");
}
}
else{
$counter_ignored++;
say("Ingored file '$file' (No image file)");
}
}
my $counter_sum = $counter_renamed + $counter_ignored;
say("Finished bulk rename");
say("Processed files:\t$counter_sum");
say("Renamed files:\t$counter_renamed");
say("Ignored files:\t$counter_ignored");
say("Closing directory...");
closedir(DIR);
 
say("Exiting");

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.