in reply to Builing a Recursive Directory Listing
use strict; use warnings; my $level = 0; processDir("."); ### # Possibly add a callback so that it doesn't only print # directories but can do other stuff too. ### sub processDir { my ($curDir) = @_; my $dirHandle = undef; opendir($dirHandle, "$curDir"); ### # Didn't feel like playing w/ regexps for a quick solution ### my @contents = sort grep { $_ ne "." and $_ ne ".." } readdir($dir +Handle); closedir($dirHandle); foreach my $file (@contents) { print "\t" x $level; print "$file"; if( -d "$curDir/$file" ) { print "/\n"; ### # Dont' follow symlinks... they cause loops ### if( ! ( -l "$curDir/$file" ) ) { ### # I should pass the level along or make it all # iterative. But you get the idea ### $level++; processDir( "$curDir/$file" ); $level--; } } else { print "\n"; } } }
p.s. use cpan modules, they are proven. this is just to show you what you asked for in native code. I wouldn't be surprised if the Find::* utils didn't do something similiar w/o recursion.. but that wasn't the point :)
Update: Grinder thought it didn't check symlinks when I was. Added a comment where the check is, + fix the -l test to actually work. On my machine, opendir() of a symlink doesn't work, but I won't say it won't on another machine. :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re:x2 Builing a Recursive Directory Listing (beware of loops)
by grinder (Bishop) on Dec 15, 2003 at 13:42 UTC | |
by exussum0 (Vicar) on Dec 15, 2003 at 13:49 UTC | |
|
Re: Re: Builing a Recursive Directory Listing
by revdiablo (Prior) on Dec 15, 2003 at 17:18 UTC | |
by exussum0 (Vicar) on Dec 15, 2003 at 18:29 UTC |