Friday, April 4, 2008

renaming files using perl

This is an example of how to rename a set of files that match a particular pattern.

#!/usr/bin/perl
use File::Find;
use File::Path;

die "usage:$0 (DIR)\n" unless @ARGV;
$dir = $ARGV[0];

finddepth(\&edits, $dir);
exit(0);


sub edits {
$fname = $_;
if (-f) {
if ($fname =~/[^\s].\.(cnf|dnf|eps|etat|planches)$/) {
print "Scanning $fname\n";
#$newname = $fname;
#$newname=~s/(.\./test_$1/;

my @values = split(/\./, $fname);
#foreach my $val (@values) {
#print "$val\n";
#}
my $pref=@values[0];
my $suff=@values[1];
$newname="test_" . $pref . "." .$suff;
print "$newname\n";
rename ($fname, $newname)

}
}
}

No comments: