This demo was given as part of the monthly question winners. This person wanted to make a Table of Contents from all documents listed in a Directory. Thanks to Alex Homer for this. Nice thing is make this your Default page and you never have to maintain your Table Of Contents for that particular directory! Copy and paste this code into a blank document, name it Default.htm and Poof! Your instant Table Of Contents is done!
Here's the code <%@ LANGUAGE=VBSCRIPT %> <% Server.ScriptTimeOut = 300 'the page may take a while to run strMenuPageURL = "/menu.htm" 'full virtual path to new menu page strListFolder = "/" 'directory holding files to be listed 'NB: List folder must end with a backslash, for example "/test/" 'You also need to make sure that the IURS_machinename accoutn has 'permission to write and delete files in the target directory %>
'create a new text file that will be the HTML menu page Set objFSO = CreateObject("Scripting.FileSystemObject") strFileName = Server.MapPath(strMenuPageURL) Set objMenuPage = objFSO.CreateTextFile(strFileName, True) 'overwrite
'write the page heading to the new menu page file objMenuPage.WriteLine "<HTML><BODY><P><B>List of files available</B></P>"
'create a collection of the files in the specified folder Set objFolder = objFSO.GetFolder(Server.MapPath(strListFolder)) Set colFiles = objFolder.Files
'read each file and extract the title to build a menu page For Each objFile in colFiles
'get the file extension and see if it's an ASP or HTML page strFileType = objFSO.GetExtensionName(objFile.Name) If (strFileType = "asp") Or (Left(strFileType, 3) = "htm") Then
'read the file contents into a string Set objStream = objFile.OpenAsTextStream(1) 'for reading strContent = objStream.ReadAll objStream.Close
'extract the title from the page if there is one strTitle = "" intStart = Instr(UCase(strContent), "<TITLE>") + 7 intFinish = Instr(UCase(strContent), "</TITLE>") If (intStart > 0) And (intFinish > intStart) Then strTitle = Trim(Mid(strContent, intStart, intFinish - intStart)) End If If Len(strTitle) = 0 Then strTitle = "Untitled page '" & objFile.Name & "'"
'create the text for a link in the menu page strThisFileURL = strListFolder & objFile.Name strLink = "<A HREF=" & Chr(34) & strThisFileURL _ & Chr(34) & ">" & strTitle & "</A><BR>" objMenuPage.WriteLine(strLink)
End If
Next
'write the closing HTML elements objMenuPage.WriteLine "</BODY></HTML>" objMenuPage.Close Response.Write "<P>Done.</P>" %>
<P><A HREF="<% = strMenuPageURL %>">Open the Menu Page</A></P>