Skip to content

Instantly share code, notes, and snippets.

@abaez
Last active December 19, 2015 10:59
Show Gist options
  • Save abaez/5944844 to your computer and use it in GitHub Desktop.
Save abaez/5944844 to your computer and use it in GitHub Desktop.
Converts digits from 00-99 or 0-9 to 000-099, of any files, in a directory tree.
#!/usr/bin/perl -w
use File::Copy;
use Cwd;
# a simple rename scheme for the files.
sub add0 {
move($_[0], "$_[1]$_[0]");
}
# check the files that will be renamed.
sub checkFile {
add0($_, "00") and ++$value if ($_ =~ m/^[0-9]\..*/);
add0($_, "0") and ++$value if ($_ =~ m/^[0-9]{2}\..*/);
}
@dirTree;
$value = 0;
opendir(DIR, getcwd()) or die $!;
# read directory for both files and if it is a directory tree
while (readdir(DIR)) {
push(@dirTree, $_) if (-d $_ and $_ =~ m/^[^\.*]/);
checkFile($_);
}
# check the dirtree for all the files
foreach my $dir (@dirTree) {
opendir(TMP, $dir) or die "ERROR on directory $dir.\n $!";
# need to chdir since no file would be found otherwise.
chdir(TMP) and print("Entered: $dir directory\n");
# check current directory and rename the files if files exist.
checkFile($_) while (readdir(TMP));
# close the TMP directory and chdir back to DIR.
closedir(TMP);
chdir(DIR);
}
closedir(DIR);
print("changed $value files\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment