Solved

Automatically create a page of links from a directory?

Posted on 1998-12-07
7
162 Views
Last Modified: 2010-04-09
I am trying to create a webpage (using Frontpage 98 and Personal Web server on Win95) that looks at a directory and automatically creates a webpage with links to each file in that directory. I am quite happy to use VBScript or Javascript or activeX controls or whatever, but I'll need the answer explained if that is the case.
0
Comment
Question by:roddymatheson
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 4

Expert Comment

by:martinag
ID: 1840290
If you've got access to Perl, try this:

#!/usr/perl

# File manager
#
# list.pl
#
# Lists a directory
#
# Input: Directory (in query string)

$sep = "\\"; #     Backslash.     Use / if Unix
$pathlength = 3; # Length of C:\. Use 1 if Unix

$allok = 0;

unless ($ENV{'QUERY_STRING'}) {
  print <<HTML;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Missing parameter</TITLE>
</HEAD>
<BODY>
<H2 ALIGN="CENTER">Missing parameter</H2>
<ISINDEX PROMPT="Please specify directory to list: ">
</BODY>
</HTML>
HTML
}
else {
  $dir = $ENV{'QUERY_STRING'};
  chop $dir if rindex($dir, $sep) == length($dir)-1 && length($dir) > $pathlength;

  $list = "";
  unless (-d $dir) {
    $title = "An error occured";
    $dirtext = "An error occured";
    $list = "</PRE>\n$dir is not a directory";
    &printRes;
    exit;
  }
  else {
    $title = "";
    $dirtext = "Current directory: $dir";
    $dirtext .= $sep if length($dir) <= $pathlength;
  }
  $cn = 0;
  opendir(DIR, $dir) or $cn = 1;
  if ($cn == 1) {
    $title = "An error occured";
    $dirtext = "An error occured";
    $list = "</PRE>\nCould not open $dir: $!";
    &printRes;
    exit;
  }
  @list = readdir DIR;
  closedir DIR;

  @dirs  = ();
  @files = ();
  while ($s = pop @list) {
    push @dirs, $s if -d "$dir$sep$s";
    push @files, $s unless -d "$dir$sep$s";
  }
  @dirs = sort @dirs;
  @files = sort @files;  # difference between a and A

  $list = "Filename"." "x(32)."Last changed  Command\n";
  while ($s = shift @dirs) {
    $display = $s;
    $display = substr($s, 0, 36)."..." if length($s) > 39;
    $url = "list.pl?$dir$sep$s";
    $url = $dir if $s eq ".";
    if ($sep eq "\\") {
      $url =~ s/(.*\\).*?/$1/g if $s eq "..";
    }
    else {
      $url =~ s/(.*$sep).*?/$1/g if $s eq "..";
    }
    $lastchanged = int((-M "$dir$sep$s") + 0.5);
    $list .= "<A HREF=\"$url\">$s</A>" . " "x(40-length($s)) . $lastchanged." days ago" . " "x(5-length($lastchanged));
    $cn = 0;
    opendir (DIR, "$dir$sep$s") or $cn = 1;
    if ($cn == 0) {
      @l = readdir DIR;
      closedir DIR;
    }
  }
  $list .= "\n" if length(@dirs) > 0;
  while ($s = shift @files) {
    $display = $s;
    $display = substr($s, 0, 36)."..." if length($s) > 39;
    $lastchanged = int((-M "$dir$sep$s") + 0.5);
    if (-r "$dir$sep$s" || -R "$dir$sep$s") {
      $list .= "<A HREF=\"$dir$sep$s\">$display</A>";
    }
    else {
      $list .= "<FONT COLOR=\"gray\">$display</FONT>";
    }
     $list .= " "x(40-length($display)) . $lastchanged." days ago" . " "x(5-length($lastchanged));
    }
  }
  $title = "FileManager - $dir";
  $allok = 1;
  &printRes;
  exit;
}

sub printRes {
  chomp $list;
  print <<HTML;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>$title</TITLE>
<STYLE>
<!--
A{text-decoration:none;color:blue}
// -->
</STYLE>
</HEAD>
<BODY>
<H2 ALIGN="CENTER">$dirtext</H2>
<PRE>
$list
</PRE>
HTML

  print "</BODY>\n</HTML>";
}

You will have to change this if you're on Unix:
$sep = "\\"; #     Backslash.     Use / if Unix
$pathlength = 3; # Length of C:\. Use 1 if Unix

This is a script of mine that I have modified and pasted in. I've removed some fancy stuff that I don't think you'll need, but I haven't tested it.

Martin
0
 
LVL 4

Expert Comment

by:martinag
ID: 1840291
Sorry, here's new:

#!/usr/perl -w

# list.pl
#
# Lists a directory
#
# Input: Directory (query string)

$sep = "\\"; #     Backslash.     Use / if Unix
$pathlength = 3; # Length of C:\. Use 1 if Unix

$ENV{'QUERY_STRING'} = "E:\\homepage\\perl";

$allok = 0;

unless ($ENV{'QUERY_STRING'}) {
  print <<HTML;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>Missing parameter</TITLE>
</HEAD>
<BODY>
<H2 ALIGN="CENTER">Missing parameter</H2>
<ISINDEX PROMPT="Please specify directory to list: ">
</BODY>
</HTML>
HTML
}
else {
  $dir = $ENV{'QUERY_STRING'};
  chop $dir if rindex($dir, $sep) == length($dir)-1 && length($dir) > $pathlength;

  $list = "";
  unless (-d $dir) {
    $title = "An error occured";
    $dirtext = "An error occured";
    $list = "</PRE>\n$dir is not a directory";
    &printRes;
    exit;
  }
  else {
    $title = "";
    $dirtext = "Current directory: $dir";
    $dirtext .= $sep if length($dir) <= $pathlength;
  }
  $cn = 0;
  opendir(DIR, $dir) or $cn = 1;
  if ($cn == 1) {
    $title = "An error occured";
    $dirtext = "An error occured";
    $list = "</PRE>\nCould not open $dir: $!";
    &printRes;
    exit;
  }
  @list = readdir DIR;
  closedir DIR;

  @dirs  = ();
  @files = ();
  while ($s = pop @list) {
    push @dirs, $s if -d "$dir$sep$s";
    push @files, $s unless -d "$dir$sep$s";
  }
  @dirs = sort @dirs;
  @files = sort @files;  # difference between a and A

  $list = "Filename"." "x(32)."Last changed\n";
  while ($s = shift @dirs) {
    $display = $s;
    $display = substr($s, 0, 36)."..." if length($s) > 39;
    $url = "list.pl?$dir$sep$s";
    $url = $dir if $s eq ".";
    if ($sep eq "\\") {
      $url =~ s/(.*\\).*?/$1/g if $s eq "..";
    }
    else {
      $url =~ s/(.*$sep).*?/$1/g if $s eq "..";
    }
    $lastchanged = int((-M "$dir$sep$s") + 0.5);
    $list .= "<BR><A HREF=\"$url\">$s</A>" . " "x(40-length($s)) . $lastchanged." days ago" . " "x(5-length($lastchanged));
    $cn = 0;
    opendir (DIR, "$dir$sep$s") or $cn = 1;
    if ($cn == 0) {
      @l = readdir DIR;
      closedir DIR;
    }
  }
  $list .= "\n" if length(@dirs) > 0;
  while ($s = shift @files) {
    $display = $s;
    $display = substr($s, 0, 36)."..." if length($s) > 39;
    $lastchanged = int((-M "$dir$sep$s") + 0.5);
    $list .= "<BR><A HREF=\"$dir$sep$s\">$display</A>";
    $list .= " "x(40-length($display)) . $lastchanged." days ago" . " "x(5-length($lastchanged));
  }
  $title = $dir;
  $allok = 1;
  &printRes;
  exit;
}

sub printRes {
  chomp $list;
  print <<HTML;
Content-Type: text/html

<HTML>
<HEAD>
<TITLE>$title</TITLE>
<STYLE>
<!--
A{text-decoration:none;color:blue}
// -->
</STYLE>
</HEAD>
<BODY>
<H2 ALIGN="CENTER">$dirtext</H2>
<PRE>
$list
</PRE>
</BODY>
</HTML>
HTML
}

Martin
0
 
LVL 10

Accepted Solution

by:
MasseyM earned 100 total points
ID: 1840292
Here it is in VBScript: (It lists every gif and jpg file.  That can be easily remove if you want)

index.asp
--------------------------------------------------------------
<html>
<head><title></title></head>
<body>

<STYLE>
TH      {
      color:Black;
      background:Silver;
      }

</STYLE>

<% SG = Request.QueryString("SG") %>

<%

Set fs = CreateObject("Scripting.FileSystemObject")       'Create the FileSystemObject
If Request.QueryString("F") = "" then
      Response.Write "<FORM METHOD=GET ACTION=index.asp>"
      Response.Write "Which Directory should I start in?&nbsp;<INPUT NAME=""F"">"
      Response.Write "<INPUT TYPE=SUBMIT VALUE=""Browse Directory""><BR>"
      Response.Write "Show Graphics as Thumbnails? <INPUT TYPE=RADIO NAME=""SG"" VALUE=1> Yes | <INPUT TYPE=RADIO NAME=""SG"" VALUE=0 CHECKED> No"
      Response.Write "<BR>View Graphic on CAROS template: <INPUT NAME=""CAROS"" VALUE=""ViewOnCAROS"" TYPE=CHECKBOX>"
      Response.Write "</FORM>"
Else
      Set fold = fs.GetFolder(Request.QueryString("F"))
Response.Write ("<TABLE WIDTH='100%'>")
Response.Write ("<TR><TD>")

 Response.Write ("      <TABLE BORDER WIDTH='100%'>")
 Response.Write ("            <TH ALIGN =LEFT><A STYLE=""text-align:left;"" HREF=" & Request.ServerVariables("HTTP_REFERER") & ">Back</A></TH>")
 Response.Write ("                  <TABLE BORDER WIDTH='100%'>")
 Response.Write ("                        <TH ALIGN = CENTER>" & fold.Path & "</TH>")
 Response.Write ("                  </TABLE>")
 Response.Write ("      </TABLE>")
 Response.Write ("</TD></TR>")

response.Write "<TABLE WIDTH='100%'><TR><TD VALIGn=TOP>"
Response.Write "<TABLE WIDTH='100%'>"
 Set foldCol = fold.SubFolders                              'Create a SubFolders collection
 For Each Y in foldCol                                    'Iterate through it
  Response.Write "<TR>"                              
  Response.Write "<TD><IMG SRC='GRAPHICS/Folder.Gif'><A HREF=index.asp?F=" & Y.Path
  If SG = "1" then Response.Write "&SG=1"
  Response.Write ">&nbsp;" &Y.Name & "</A>"
  Response.Write "</TD>"
  Response.Write "</TR>"
 Next
  Response.Write "</TABLE>"

Response.Write "</TD><TD>"

response.Write "<TABLE WIDTH='100%'>"
 Set foldCol2 = fold.Files                              'Create a Files collection
 For Each Y in foldCol2
 Cur = Cur + 1                                    'Iterate through it
  If Cur >= 6  then Response.Write "<tr>"      
  Response.Write "<td WIDTH='20%'>"
If Right(Y.Path,3) = "gif" or Right(Y.Path,3) = "jpg" then
  If SG = "1" then
      Response.Write "<IMG WIDTH =50 HEIGHT=50  SRC=" & Y.Path &" ><BR>"
  End If
  Response.Write "<A HREF=ViewasBG.asp?G=" & Y.Path & "&CAROS=" & Request("CAROS")
  If SG = "1" then Response.Write "&SG=1"
  Response.Write ">" &Y.Name & "</A>"            'Print the files name
End If
  Response.Write "</td>"
  If Cur >= 5 then
      Response.Write "</tr>"
      Cur = 0
  End If
 Next                                                      
 Response.Write "</table>"
 For Each Y in foldCol                                    'Now lets iterate through the folder collection again
 Next
Response.Write "</TABLE>"
Response.Write "</TD></TR></TABLE>"
End If
%>


ViewAsBG.asp
--------------------------------------------------------
<% Graphic = Request.QueryString("G") %>
<% UT = Request.QueryString("UT") %>

<% If Request.QueryString("CAROS") <> "" then %>
      <!-- #Include Virtual="/CRM/CAHeader.asp" -->
      <CENTER><FONT COLOR="RED" SIZE=+3>T E M P L A T E   V I E W</FONT><BR>
            <IMG SRC="<% =request.querystring("G") %>"><BR>
      <A HREF="javascript:history.back(1);">Back</A></CENTER>
      <!-- #Include Virtual="/CRM/CAFooter.asp" -->

<% Else %>
      <% ML=Request.QueryString("ML") %>

      <!-- #Include File="sample2.asp"-->
      <CENTER><A HREF="javascript:history.back(1);">Back</A></CENTER>
<% End If %>
</BODY>

Hope that helps:

0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:roddymatheson
ID: 1840293
masseym - The VBScript solution is obviously close but we get a VBScript error in the ASP saying that GetFolder method is not supported.

martinag - Sorry no access to Perl and the data is on 95 anyway. Thanks
0
 
LVL 10

Expert Comment

by:MasseyM
ID: 1840294
What are your versions?
IIS:
VBScript Engine:
System:
etc...

Also, where it says "GetFolder(Request.QueryString("F")), is there a Parameter called F in the address above?  Try this, replace request.querystring("F") with a path that you KNOW is good.  It should not fail now.
0
 
LVL 4

Expert Comment

by:chabaud
ID: 1840295
Another simple answer: Create a static html file using a .BAT !

==========LST.BAT==========
@echo off
echo"<html><body bgcolor=Black><center><!--">lst.htm
for %%i in (yourpath\*.jp*; yourpath\*.gif) do call lst_s %%i
echo"--></center></body></html><!--">>lst.htm
echo"-->">>lst.htm

==========LST_S.BAT==========
@echo %1
echo"--><a href=%1><img src=%1 alt=%1></a><!--" >>lst.htm

0
 
LVL 1

Author Comment

by:roddymatheson
ID: 1840296
I downloaded the latest Script engine (3.1) from MS and it worked. Excellent. Thanks
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now