TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:14107] Need help with Perl program



"Mike Glaser" <jurupari@geocities.com> writes:

> I am trying to learn Perl and I am having trouble solving what I 
> would think should be a simple problem.
> 
> What I would like to do is create an array that has in it all of the 
> files in a directory and its subdirectories. To put it another way, I 
> am looking for the output of 'ls -R' placed into an array more or 
> less.

Look at readdir and opendir, if you have spaces or other special characters in
the file/directory names you'll get screwed with the standard <*.html> wrapper
stuff.  Then do something like this:

#!/usr/bin/perl

method("$startDir");

sub method {
opendir DIR, shift
while(readdir(DIR)) {
  my $file = $_;
  if(-d $file) {
    method($file);
  }
  else if($file =~ /$searchString/) {
    #make $newFilename mangle the filename to the new one
    File::Move:($file, $newFilename)
  }
}
}

This is by no means tested, but gives you the rough idea.  You might want to
make sure your directory handles are local, otherwise the recursion will hit
you.

-- 
Jon Schewe 
http://eggplant.mtu.net/~jpschewe
schewe@tcfreenet.org