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

Hey,

Yep its me again... Need a little advice on this. I am creating a system that will read in a dorectory structure. It will first print the first layer of the directory tree. Then when you click on the link it will scan the directory you selected and print hte directory tree in there... You then select your next directory and it then prints the files in it...

Now my question is... Are there modules that will do this? Is there already a script out there that will do this (you know why reinvent the wheel if it is already done)? Umm and any other suggestions you got??

Regards,
Billy S.

Replies are listed 'Best First'.
Re: Directory Display and File Print out
by Agermain (Scribe) on Aug 09, 2001 at 19:43 UTC

    This should be enough to get you started. Caveats: . and .. are not real directories, but are used by the system. Also, if you make these clickable, you shouldn't allow the user to back up past the root directory (basedir, which you should change to suit your system) by feeding req_dir with too many ..s.

    Other recommendations: For getting this into a web-based form, you'd want to make the directories clickable, by printing HTML <A> tags around the directories, and then feed the results back to your script with CGI variables. As it is right now, the script below is less useful than the DOS dir command.

    my $basedir = '/usr/data/www/you'; ## This is the directory to start +with. my $req_dir = ''; ## This is the subdirectory you want, relative to $ +basedir opendir(DIR, "$basedir/$req_dir") || die "can't opendir $req_dir: $!"; @dir_records = readdir(DIR); @dir_records = sort @dir_records; closedir DIR; $file_count = 0; @dir_dirs = grep {-d "$basedir/$req_dir/$_"} @dir_records; # this stuf +fs @dir_dirs with the directories foreach $thisdir (@dir_dirs) { print "$thisdir (directory)\n"; } foreach $_ (@dir_records) { if (-f "$basedir/$req_dir/$_") { print "$_\n"; } }

    andre germain
    "Wherever you go, there you are."

      Hi Andre , I was reading ur script and this is really what will help me out , can u please if possible mail me the functional code for displaying the directories on a webpage and to make them clickable .. I have been writing soemthing to do that in cgi-perl .. but it's not coming up on how i want it to be .. but what u have written will be wonderful if you can mail it to me . Thanks Asheesh_ch@hotmail.com
Re: Directory Display and File Print out
by RatArsed (Monk) on Aug 09, 2001 at 18:43 UTC
    Er, I may be mis-interpreting your question, but will glob() help you? Or do you want to display the entire tree up to the selected node?

    --
    RatArsed