iman_saleh has asked for the wisdom of the Perl Monks concerning the following question:
use Cwd; # module for finding the current working directory $|=1; # turn off I/O buffering # This subroutine takes the name of a directory and recursively scans # down the filesystem from that point looking for files named "core" sub ScanDirectory{ my ($workdir) = shift; print "Work dir = $workdir\n"; my ($startdir) = &cwd; # keep track of where we began chdir($workdir) or die "Unable to enter dir $workdir:$!\n"; opendir(DIR, ".") or die "Unable to open $workdir:$!\n"; my @names = readdir(DIR) or die "Unable to read $workdir:$!\n"; closedir(DIR); foreach my $name (@names){ next if ($name eq "."); next if ($name eq ".."); if (-d $name){ # is this a directory? &ScanDirectory($name); next; } else { # this is a file print "Name = $name\n"; $newName = $name; $newName =~ s/\..*//; $newName .= ".xml"; $result = rename($name, $newName) or die "cannot rename $name to $newName:$!"; } chdir($startdir) or die "Unable to change to dir $startdir:$!\n"; } } &ScanDirectory($ARGV[0]);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl rename method
by FunkyMonk (Bishop) on Dec 24, 2007 at 18:19 UTC | |
|
Re: Perl rename method
by ikegami (Patriarch) on Dec 24, 2007 at 18:39 UTC |