Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: script for directory mapping

by CombatSquirrel (Hermit)
on Aug 25, 2003 at 21:21 UTC ( [id://286502]=note: print w/replies, xml ) Need Help??


in reply to script for directory mapping

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://286502]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-19 13:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found