how to rename file extensions in perl within sub-directories
by Mahdi Sajjadpour

this is a quick nifty script to change extension names for a set of files within subdirectories. e.g. *.jad to *.java. Im sure there are tons of awk versions out there, but this works for me.

script changes *.jad to *.java starting from the current directory. You can ofcourse change all that....

#!/usr/bin/perl

use File::Basename;
&read_dir(".");

sub rename_file {

$file = basename($path);
$directory = dirname($path);

if( $file =~ /((\w+)\.jad)/ ) {

print "renaming $path to $directory/$2.java\n";
system("/bin/mv $path $directory/$2.java");

}

}


sub read_dir{

local($dir) = shift;
local($path);
unless (opendir(DIR, $dir)) {
warn "Can't open $dir\n";
closedir(DIR);
return;
}
foreach (readdir(DIR)) {
next if $_ eq '.' || $_ eq '..';
$path = "$dir/$_";
if (-d $path) { # a directory
&read_dir($path);
} elsif (-f _) { # a plain file
&rename_file($path);
# or do something you want to
}
}
closedir(DIR);



}

Copyright by techTips