HOWTO: Use ASP and Scripting.FileSystemObject to Create a Dynamic Table of Contents Page

ID: Q218606


The information in this article applies to:


SUMMARY

Active Server Pages (ASP) can be used to create a dynamic Table of Contents for a Web site that is updated frequently. Using Active Server Pages makes it easier to keep Web sites up to date without having to manually update a contents page.


MORE INFORMATION

WARNING: ANY USE BY YOU OF THE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides this code "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

The following ASP code is designed to create a Table of Contents from the files listed in a subfolder named "docs." To use this example, copy the ASP code and save it as a file named "contents.asp" in a folder that has at least "Script" access. To alter the location of the folder to display, change the line of code that defines the strDocsPath variable.

<%@LANGUAGE="VBSCRIPT"%>
<%
	Option Explicit	

	Dim strDocsPath, strDocsPhysicalPath
	Dim objFSO, objFolder, objFiles, objFile
	Dim strName, strFile, strType, lngSize

	' NOTE: set the following line to the folder to display
	strDocsPath = "docs"

	' map the folder to a physical path
	strDocsPhysicalPath = Server.MapPath(strDocsPath)

	' create a system file object
 	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

	' create an object for the folder
	Set objFolder = objFSO.GetFolder(strDocsPhysicalPath)

%>
<html>
<head>
<title>Table Of Contents</title>
</head>

<body>

<h1 align="center">Table Of Contents</h1>

<h4>Please choose the Document to view.</h4>

<ul>
<%
	' create a files collection
	Set objFiles = objFolder.Files

	' step through the files collection
	For Each objFile in objFiles

		' get a file's name
		strName = objFile.Name

		' make it lowercase for the URL
		strFile = Lcase(strName)

		' get the file's type
		strType = objFile.Type

		' make the name a title for display
		strName = MakeTitle(strName)

		' get the file size in KB
		lngSize = objFile.Size\1024

		' output the filename and URL
		Response.Write "<li><a href=""" & strDocsPath & "/" & strFile & """>" & strName & "</a><br>"

		' output the file's size and type
		Response.Write "<em>(" & lngSize & "KB " & strType & ")</em></li>" & vbCrLf
	Next

	' this function simply drops the extension from a file
	Function MakeTitle(strTemp)
		If InStrRev(strTemp,".") Then
			strTemp = Left(strTemp,InStrRev(strTemp,".")-1)
		End If
		MakeTitle = strTemp
	End Function
%>
</ul>
</body>
</html> 
For more information on Microsoft Scripting Technologies, please see the following URL:

http://msdn.microsoft.com/scripting/

Additional query words:


Keywords          : 
Version           : winnt:4.0,5.0
Platform          : winnt 
Issue type        : kbhowto 

Last Reviewed: March 31, 1999