Thanks Bill,

Your method is very nice. I should have realized the paths weren't going to
maintain consistency when I was doing this.

-Wes

-----Original Message-----
From: $Bill Luebkert [mailto:[EMAIL PROTECTED]] 
Sent: Monday, March 18, 2002 8:48 PM
To: 'Activeperl (E-mail)
Subject: Re: Weird issue with a directory test


Steven Stubbs wrote:

> hello Wes,
> 
>       I've never used File::Find before so I tried your script and found a
> problem. Files in subdirectories are not given their full paths. Just 
> $SearchDir and the filename, no subdirectory name.
> 
>       Also, is there a reason for setting $File? I don't see it used.
> 
>                                               Steven
> 
> PS. I only just read your thread this evening so I am joining it late. 
> Here
> is a recursive sub using opendir and readdir. Pass the sub the directory 
> you want searched and it will fill up two global arrays, @dirs holds the 
> directory paths and @files holds the file paths.
> I like your way of find . and .. better.
> 
> 
> 
> sub getdir
> {
>   $inpath=$_[0];
> 
>   my (@localdirs)=();
> 
>   opendir(DIR,$inpath) or die "can't opendir $inpath: $!";
>   while(defined($file=readdir(DIR)))
>   {
>     $pathfile=$inpath."\\".$file;
>     unless (($pathfile eq "$inpath\\.") or ($pathfile eq "$inpath\\.."))
>     {
>       if (-d "$pathfile")
>       {
>         push(@dirs,$pathfile);
>         push(@localdirs,$pathfile);
>       }
>       else
>       {
>         push (@files,$pathfile);
>       }
>     }
>   }
>   closedir(DIR);
>   if (@localdirs != 0)
>   {
>     foreach $outpath (@localdirs)
>     {
>       getdir($outpath);
>     }
>   }
> }


Slightly simpler/cleaner:


use strict;

my @dirs;
my @files;

recurse_tree ('.');
print "dirs:\n"; foreach (@dirs) { print "$_\n"; }
print "files:\n"; foreach (@files) { print "$_\n"; }
exit;

sub recurse_tree {
        my $dir = shift;
        local *DIR;

opendir DIR, $dir or die "can't opendir $dir: $!";
while ($_ = readdir DIR) {

        next if /^\.{1,2}$/;
        my $path = "$dir/$_";
        if (-d $path) {
                push @dirs, $path;
                recurse_tree ($path);
        } else {
                push @files, $path;
        }
}
closedir DIR;

}

__END__

or I like this one a little better:


# recurse the tree to a hash - no global usage - args are passed

use strict;

my %tree;
my $root = '.';

recurse_tree (\%tree, $root);

print "tree starting at '$root':\n";
foreach my $dir (sort keys %tree) {
        print "$dir:\n";
        foreach (sort @{$tree{$dir}}) { print "\t$_\n"; }
}
exit;

sub recurse_tree {
        my $tree = shift;
        my $dir = shift;
        local *DIR;

opendir DIR, $dir or die "can't opendir '$dir': $!";
while ($_ = readdir DIR) {

        next if /^\.{1,2}$/;
        my $path = "$dir/$_";
        if (-d $path) {
                $tree->{$dir} = [];
                recurse_tree ($tree, $path);
        } else {
                push @{$tree->{$dir}}, $_;
        }
}
closedir DIR;

}

__END__



-- 
   ,-/-  __      _  _         $Bill Luebkert   ICQ=14439852
  (_/   /  )    // //       DBE Collectibles   Mailto:[EMAIL PROTECTED]
   / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic
http://www.todbe.com/

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to