To see this implemented without any modules, you can look at the following code (look at my note at the end, though):
#!perl
use strict;
use warnings;
sub printDir {
my $headDir = shift; # get starting directory
our $indent = 3; # indentation
our $increase = 3; # indentation steps
sub recurseDir {
my ($home, $dir) = @_; # get home directory and current di
+rectory
unless (-d "$home\\$dir") { # if it is a file
print " " x $indent . "$dir\n"; # just print its name
return;
}
print " " x $indent . "[$dir]\n"; # it's a dir, so print its nam
+e fancily
opendir(DIR, "$home\\$dir") # let's have a look at the dir
+'s contents
or die "Failed to open '$home\\$dir': $!\n";
my @filelist = readdir DIR; # get them
closedir DIR; # close the handle
$indent += $increase; # increase indentation for nex
+t recursion level
for (@filelist) { # iterate through dir contents
next if ($_ eq '.' or $_ eq '..'); # ignore if . or ..
recurseDir("$home\\$dir", $_); # and recurse otherwise
}
$indent -= $increase; # restore indentation
}
unless (-d "$headDir") {
print " " x $indent . "$headDir\n";
return;
}
print " " x $indent . "[$headDir]\n";
opendir(DIR, $headDir) or die "Failed to open '$headDir': $!\n";
my @filelist = readdir DIR;
closedir DIR;
$indent += $increase;
for (@filelist) {
next if ($_ eq '.' or $_ eq '..');
recurseDir($headDir, $_);
}
}
printDir 'C:\Dokumente';
In real life, however, (if it exists ;-) you should
always use File::Find or a similar module to make your code more portable and easy to read. Furthermore ther are some traps, like links and such, which might render this simple solution useless. Conclusion:
Always use File::Find or a similar module.
Hope this helped.
CombatSquirrel.
Entropy is the tendency of everything going to hell.