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
| [reply] [d/l] |
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...
| [reply] [d/l] |
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>";
}
| [reply] [d/l] |
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
| [reply] |