in reply to Directory Display and File Print out

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."

Replies are listed 'Best First'.
Re: Re: Directory Display and File Print out
by Anonymous Monk on Apr 29, 2003 at 16:08 UTC
    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