Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Automatically create a page of links from a directory?

Posted on 1998-12-07
7
166 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
div to fit another div 8 28
Save ms data to server side. 19 55
Mouse event to control image and transparency. 4 33
..ignore the Question 1 6
Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
This article discusses four methods for overlaying images in a container on a web page
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

839 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