in reply to Descending a directory tree, returning a list of files
See also How do I traverse a directory tree?use strict; use warnings; use Cwd 'getcwd'; ## cwd my $cwd = getcwd; ## Get files from cwd my $res = _list_files($cwd); sub _list_files { my $dir = shift; my $files = shift; opendir(my $dh, $dir) || die "Error opening: $dir\n"; while (my $fn = readdir($dh)) { ## skip hidden next if $fn =~ /^\./; ## file, fullpath name my $file = $dir . "/" . $fn; ## add push(@{ $files }, $file); ## dir, decend if (-d $file) { ## sub-directory files $files = _list_files($file, $files); } } #closedir($dh); # Not necessary; auto-closed on end of scope return $files; }
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Descending a directory tree, returning a list of files
by Rodster001 (Pilgrim) on Jun 09, 2015 at 20:22 UTC | |
by kennethk (Abbot) on Jun 09, 2015 at 21:32 UTC | |
by Rodster001 (Pilgrim) on Jun 09, 2015 at 22:55 UTC |