• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Automatically create a page of links from a directory?

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
roddymatheson
Asked:
roddymatheson
  • 2
  • 2
  • 2
  • +1
1 Solution
 
martinagCommented:
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
 
martinagCommented:
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
 
MasseyMCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
roddymathesonAuthor Commented:
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
 
MasseyMCommented:
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
 
chabaudCommented:
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
 
roddymathesonAuthor Commented:
I downloaded the latest Script engine (3.1) from MS and it worked. Excellent. Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now