Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#!/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)");
if (move("$dir/$file", "$dir/$new_file")){
say("Renamed file '$file' -> '$new_file'");
say("Couldn't rename file '$file': $!");
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...");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment