Solved

Automatically create a page of links from a directory?

Posted on 1998-12-07
7
164 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
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to Left-Align Navigation bar? 4 26
maps stopped work unsure why 7 35
Html5 string 3 23
Changing alignment and creating border 6 29
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

809 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