Generating HTML Links from Folder Contents
Peter Wone - GUI Computing
Most web servers will generate a page of links if you attempt to browse a URL referring to a folder (directory) not containing a file called index.html. Some support the specification of files containing headers, footers and various other things which are used to structure the page and annotate the listed files, as well as ways to hide files. The methodologies in use vary and, in any case, are largely fixed. You can't, for example, apply a style sheet to the generated links page.
In response to a need to customise this function, I wrote some Delphi object classes and a CGI program which uses them to achieve an automated links page.
TFolderContentsList
There are also the properties Strings and Sorted which are inherited from TStringList.
Strings is the default property of TFolderContentsList which means that if you have a variable AFolderContentsList of type TFolderContentsList, then AFolderContentsList[0] is the same as AFolderContentsList.Strings[0].
Strings will contain the names of all the files matching Filemask in the folder specified in DosPath. If you assign a value to RelativeURL which resolves to a valid folder, then Strings will contain the names of the files in the folder which match Filemask.
If there aren't any files in the folder matching Filemask then Strings will contain one item - 'No matching entries'. If RelativeURL doesn't resolve to a valid folder, Strings will contain one item - 'Invalid folder'.
Status reports the error status of the object, allowing you to easily ascertain whether an error has occurred. Note that 'No matching files' is not reported as an error. The error codes reported by Status are the standard Windows codes passed through. An exception is error 18 (no more entries to retrieve) which is reported as 0 (no error) to simplify error checking, since the looping is completely hidden from and irrelevant to any code inheriting from, or otherwise using this object class.
How TFolderContentsList works
When the object is created, GetDocumentRoot is used to populate FDocumentRoot with the DOS path equivalent for the '/' URL of the web server invoking it.
When you assign a value to RelativeURL, the property procedure AFolderContentsList.SetRelativeURL uses this information to generate and write, into the appropriate properties, the complete equivalent DOS path and the complete absolute URL. Along the way, slash-direction fixups and trailing slash fixups are performed. When the DOS path is available, it is used to populate the Strings property via a private method GetFolderContents.
TFolderContentsLinksList
<A HREF="relative-url/filename.ext">file name</A>
There are no new methods or properties.
TFolderContentsLinksTable
How TFolderContentsLinksList works
TFolderContentsLinksList exports the Filemask, RelativeURL, AbsoluteURL, DosPath and Status properties by wrappering them - Get/Set methods are implemented which refer internally to the properties of an internally declared instance of TFolderContentsLinksList.
The code is long. Click here to download genlnk10.zip a zipped Delphi project (27 KB).
I have included both a test harness and a program using this code which compiles into a CGI program suitable for an O'Reilly Web Site web server. The Web Site specific code pertains entirely to discovering the document root folder (the directory which forms the root of your web) and is contained in GetDocumentRoot.
I favour the O'Reilly web server because the same program runs happily on either a WinNT server or (!!!) on a Win95 workstation. You can obtain a trial copy from http://software.ora.com/, and I must add that Web Site Pro can use ISAPI DLLs directly.
The CGI program compiles into GenFolderLinks.exe which you must place into the …\cgi-win\ folder of your web server. By default this will be c:\website\cgi-win\. Several support files are required before you can compile this program. I have included the necessary DCU files but I must credit them to Michael B. Klein whom you can contact at: mbk@baldrick.com, or on CompuServe 74323,3555.
I encourage you to obtain his full Internet Server Extension Expert for Delphi 2.0 as it's an absolute must for the Delphi developer working with either CGI or ISAPI. Using the Server Extension Expert I found it effortless (once I understood the technologies involved) to create CGI and ISAPI server extensions, and the way he's put it together, it's a cakewalk to turn a CGI app created with it into an ISAPI DLL.
I suggest you begin by creating a CGI application because they're much easier to work with during debug. Then convert it, which is easy. Just run the Server Extension Expert again and then remove the unit1 it created from the program and add the CGI unit you just debugged. Voila! Instant ISAPI with no heartbreak or ulcers.
While we're marveling at my (and Michael's) general cleverness, notice how I get a long string to display wrapped. TMemo objects keep their text in a TStringList. To copy a string to the TStringList, all you need to do is (a) clear the list using its Clear method, then (b) assign the string to element zero, which always exists -
AMemo.Clear;
AMemo[0] := AString;