Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.