Dynamically create a Hyperlinked Menu (Table of Contents) from all files listed in a directory.

Contributed by
Rating: 4 stars4 stars4 stars4 stars4 stars / 9
October 27, 1999
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

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
%>

<HTML>
<BODY>

<%
Response.Write "<P>Creating menu page: " & strMenuPageURL & " ...</P>"

'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>
 

blog comments powered by Disqus
ASP CODE ARTICLES

- ASP Forms
- ASP: The Beginning
- Getting Remote Files With ASP Continued
- Inbox and Outbox Manipulation in ASP
- Relational DropDownList Using VB.NET
- Ad Tracking URL Hits
- Use ViewState to display one record per page...
- Send Email using ASP.NET formatted in HTML
- ASP File Explorer
- ASP/XML Interview questions by Srivatsan Sri...
- Pressing RETURN won't submit the form
- This shows how you get the TEXT of a combo r...
- Group Data by Adrian Forbes
- Multiple checkbox select sample
- Multiple checkbox select with all values sam...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials