Web Automation: Dynamic Directory Indexing Page 4

By Matthew Keller (Send Email)
Posted Jun 2, 2000


More robust HTML for prettiness

  • Making each entry a "list item"
  • Exclusion list--an array of folders to skip during indexing
  • Case-insensitive alphabetical directory sorting
  • More memory-efficient iteration code
  • Better error trapping for file/directory opening
  • Case-insensitive alphabetical title sorting
  • Recurse into sub-directories and "tree" out (like a spider)
  • Allow the user to sort the list by clicking on Sort by Title, Sort by Age
  • Allow the user to Sort by Popularity (based on the number of "hits")
  • I'd also like to shamelessly plug Lincoln Stein's CGI.pm module, which is available on CPAN, and probably is already in your Perl distribution. I use this module to take a lot of the HTMLizing out of my hands. I wrote this example using no modules, so you could see what was going on, but I highly recommend using CGI.pm to do a lot of the HTML stuff for you.

    Full Text of this Example

    1 : #!/usr/bin/perl -w
    2 :
    3 : sub Get_Title {
    4 : my =shift;
    5 : unless(-f "") { return("NO INDEX"); }
    6 : open(HTML,"<");
    7 : while(<HTML>){
    8 : if(sh=~ /<title>(.*)<\/title>/i) {
    9 : close HTML;
    10: return "";
    11: }
    12: }
    13: close HTML;
    14: return "Untitled";
    15: }
    16:
    17: my ="/usr/local/apache/htdocs/projects/";
    18: my ="http://mattwork.potsdam.edu/projects/";

    19: opendir(PRJD,"");
    20: my @dirs=readdir PRJD;
    21: closedir(PRJD);
    22: print "Content-Type: text/html\n\n";
    23: print "<html><head><title>Project Index Page</title></head><body>\n";
    24: for(@dirs) {
    25: if(sh=~ /^\./) { next; }
    26: unless(-d sh) { next; }
    27: my ="sh/index.html";
    28: my =Get_Title("");
    29: =~ s///i;
    30: print "<a href=\ "\ "></a>\n";
    31: }

    Matthew Keller specializes in server technologies, with bias towards Linux, Solaris and the Apache Web server. Besides working for SUNY Potsdam, he provides numerous consulting services, has co-authored a couple books about Apache, and even manages to wrestle a mountain every now and then. Visit his personal Web site.

    Page 4 of 4


    Comment and Contribute

    Your name/nickname

    Your email

    (Maximum characters: 1200). You have characters left.