[oak perl] file renaming script

Michael Paoli mp at rawbw.com
Wed Feb 16 07:13:52 PST 2005


In a word: recursion

E.g. you could create a subroutine which handles renaming items within
a directory, where the directory is passed to the subroutine, and where
the subroutine, upon encountering a directory within the directory
passed to it, calls itself (recursion), passing that (sub-)directory
information.  This will also exercise your scoping skills a bit. :-)

Quoting Sandy Santra <santranyc at yahoo.com>:

> I've used this script to rename directories on a Win98 machine.  (Apologies
> 
> in advance for not scoping my variables.)  Is there a line of code or 
> switch or something I can add that would make it *also* operate on every 
> file within each directory it's processing?  Thanks.
> 
> ## find [text string] and delete it from all file and directory names in a 
> directory
> 
> chdir "c:/[directoryname]" or die "cannot chdir to that directory: $!";
> foreach $file (glob "*") {
>          $newfile = $file;
>          $newfile =~ s/%2f/_/;
>          if (-e $newfile) {
>                  ## warn "can't rename $file to $newfile: $newfile
> exists\n";
>          } elsif (rename $file, $newfile) {
>                  ## success, do nothing
>          } else {
>                  warn "rename $file to $newfile failed: $!\n";
>          }
> }




More information about the Oakland mailing list