in reply to File::Find exit
This code does a width first traversal without recursion and returns some results you may find easier to work with. All it does is iterate over a list of dirs. Starting at the root dir it just keeps pushing the subdirs it finds onto the end of this list, thus the width first traversal.
# recurses the directory tree with the root as the arg # returns paths relative to root -> not absolute paths # does not return root dir itself in @dirs list # returns references \@dirs and \@files and \%tree sub recurse_tree { my $root = shift; my @dirs = ( $root ); my @files; my %tree; for my $dir ( @dirs ) { opendir DIR, $dir or die("Can't open $dir\n"); # could just ne +xt (my $rel_dir = $dir) =~ s!^\Q$root\E/?!!; while ( my $file = readdir DIR ) { next if $file eq '.' or $file eq '..'; next if $file =~ m/^_/; # skip _ prefix files and dirs next if -l "$dir/$file"; # don't follow sym links push @dirs, "$dir/$file" if -d "$dir/$file"; push @files, "$dir/$file" if -f "$dir/$file"; push @{$tree{$rel_dir}}, $file if -f "$dir/$file" and $fil +e =~ m/\.html?$/; } closedir DIR; } # make paths relative to $root, comment out for full path @dirs = grep { $_ and ! m!^/$! } map{ s!^\Q$root\E/?!!; $_ } @dirs +; @files = map{ s!^\Q$root\E/?!!; $_ } @files; return \@dirs, \@files, \%tree; }
cheers
tachyon
|
|---|