From: Holly S. <hs...@mi...> - 2000-07-05 18:31:22
|
On Wed, 5 Jul 2000, Michael wrote: > Sigh..I assume no one has done this then because most you guys are > very helpfull and quick on response...:( well, anyone ever read a > harddrive directory and make a text file out of it with a PL script? I > am just trying to find a starting point because I never tried this stuff > with perl. Assuming you want to read one directory (rather than reading recursively), this should do the trick (tested in Solaris). It's not the cleanest it could be, and represents just how I thought about doing this off the top of my head... and can probably be made much more compact, but it should give you an idea of how to go about your task in perl (a creative starting point, if you will). Let me know if it works OK for ya! -Holly, verbose with comments #!/usr/local/bin/perl # or point to wherever your perl engine lives # relative to location of CGI $path = "../"; opendir HERE, $path or die "Can't read this dir because: $!"; @files = readdir HERE; closedir HERE; # If you want to call the CGI directly, you can. If you want to have # The CGI print the contents out to a text file, and then point you at # that text file (web page) you can. Just chose which subroutine you # would like to use, and comment the other one out. #call_cgi_direct(); print_and_redirect(); sub call_cgi_direct { print "Content-type: text/html\n\n"; # Required, invisible to web browser print "<html><head><title>Contents of the directory</title></head>\n"; print "<body bgcolor=ffffff>\n"; print "<center>\n"; list_files(); print "</center>\n"; print "</body>\n"; print "</html>\n"; } sub print_and_redirect { $openup = $path."listing.html"; open( HTML, ">$openup"); print HTML "<html><head><title>Contents of the directory</title></head>\n"; print HTML "<body bgcolor=ffffff>\n"; print HTML "<center>\n"; list_files_redir(); print HTML "</center>\n"; print HTML "</body>\n"; print HTML "</html>\n"; close(HTML); print "Location: http://www.url.to/your/listing.html\n\n"; } sub list_files { # Now that we have the contents of all files in the @files list, # you can manipulate them to display whatever you want. Below is a # pattern match for any file ending in ".htm" or (".html" or ".htmx" # where "x" is any alphanumeric character... the match is against 1 or # 0 "x"es)... the i after the match makes it case insensitve, so that # both .htm and .HTM and .HtML will match foreach $file (@files) { # Cycle through the directory contents if ($file =~ /\.htm\w?$/i && $file !~ /listing\.html/) { print "<a href=\"$path$file\">$file</a><br>\n"; } } } # Same as above subroutine, except it writes to a file, rather than # printing out directly back to the browser (STDOUT) sub list_files_redir { foreach $file (@files) { # Cycle through the directory contents if ($file =~ /\.htm\w?$/i && $file !~ /listing\.html/) { print HTML "<a href=\"$file\">$file</a><br>\n"; } } } |