spacey has asked for the wisdom of the Perl Monks concerning the following question:

Hello and good morning to your all.
I would like to thank your all for your help with this problem.
A special thank you to Kanji for his example of pre and post in File::Find
It was more then helpful.
Likewise thanks for taking the time to submit your own version of my shabby code.

Hopefully I can return the favour someday.
Regards,
Gareth

Replies are listed 'Best First'.
Re: Directory Tree
by zentara (Cardinal) on Mar 26, 2004 at 16:31 UTC
    Maybe this will give you some ideas?
    #!/usr/bin/perl use warnings; use strict; print<<EOH; Content-type: text/html <html><body> EOH my $dir = shift || '.'; opendir my $dh, $dir or die "Error: $!"; my @files = grep !/^\.\.?$/, readdir $dh; closedir $dh; print "<P> <UL> \n"; foreach my $f(@files) { if(-d "$dir/$f") { print "<LI> <a href=\"$f\"> $f </a> \n"; print "<UL>\n"; opendir my $dh, "$dir/$f" or die "Error: $!"; my @filesub = grep !/^\.\.?$/, readdir $dh; closedir $dh; foreach my $s(@filesub) { print "<LI> <a href=\"$f/$s\"> $f/$s </a>\n"; } print "</UL>\n"; } else { print "<LI> <a href=\"$f\"> $f </a>\n"; } } print "</UL> <P>\n";

    I'm not really a human, but I play one on earth. flash japh
Re: Directory Tree
by Kanji (Parson) on Mar 26, 2004 at 18:09 UTC

    If you're using a version of File::Find that supports pre- and postprocess hooks (>= Perl 5.6, IIRC) then you can do something like...

        --k.


Re: Directory Tree
by jaa (Friar) on Mar 26, 2004 at 19:45 UTC
    Here's an example that does no recusion of its own - it leaves that to File::Find. Instead, it puts everything into an array, and iterates through the array, detecting when a change of dir happens, by looking at the depth of the individual item. Probably as clear as mud? :)

    It doesn't use File::Basename either, but does depend on you knowing the folder delimiter for your platform ('/' for me)

    #!/usr/local/bin/perl -w use strict; use File::Find; sub logGeneral { print @_, "\n";} my $delim = '/'; my $padding = ' '; my $depth = 0; my $ltype = 'OL'; my $dir = $ARGV[0]; $dir ||= '.'; # sorted array of file and folder names our @files; sub proc { push @files, [ split $delim, $File::Find::name ]; } find(\&proc, $dir); # format it as a list! my $lastdepth = 0; for my $file ( @files ) { if ( scalar @$file > $lastdepth ) { # first entry in a sub-folder logGeneral $padding x $depth, "<$ltype>"; $depth++; logGeneral $padding x $depth, "<LI>$file->[$#$file]"; } else { if ( scalar @$file < $lastdepth ) { # have left a sub-folder # may have jumped back several levels for my $i ( 1 .. $lastdepth - scalar @$file ) { $depth--; logGeneral $padding x $depth, "</$ltype>"; } } logGeneral $padding x $depth, "<LI>$file->[$#$file]"; } $lastdepth = scalar @$file; } # finish off from the last entry for my $i ( 1 .. $lastdepth ) { $depth--; logGeneral $padding x $depth, "</$ltype>"; }

    Regards,

    Jeff

Re: Directory Tree
by spacey (Scribe) on Mar 26, 2004 at 17:03 UTC
    Many thanks to zentara.
    Your code has given me much food for thought.
    Unfortunately It dose not appear to be a recursive search.
    As soon as it indexes the first bunch of dirs it stops.
    Your example should give me enough scope to build a recursive version.
    So again thanks for your input.

    Gareth