Selecting a Random File from a Directory and Outputting its Contents Demo

This demo illustrates how to have a random text file read from a directory and have its contents displayed. Below you will see the output of a random text file from the directory of (very) old WebWeekly issues.

WebWeekly: Your weekly source for Web Technology Tips and Tricks! -Forward this email and subscription information to a fellow developer!******************************************************************************************************************************************** ------ HOT ASP COMPONENT MAKES BROWSER COMPATIBILITY EASY! ------Did you know? BrowserHawk 2.0 allows you to easily create web sitesthat work consistently for the wide variety of browsers in use today.It recognizes all IE and NN browsers, providing info on more than 40capabilities including DHTML, FileUpload, StyleSheets, SSLKeySize, andeven detects disabled cookies!Visit now to download your FREE evaluation!******************************************************************************************************************************************** ********************************************************************** Implementing an On-line Help System***********************************************************************'t it be nice to have a nifty on-line help system on your website?This help system would need to be fast loading, easy to navigate, pleasanton the eyes, and, from an administrative perspective, easy to update.Well, look no further, for I am about to show you how to create a killerhelp system that's as easy as pie. First things first, we don't need no stinkin' databases here! We will beusing the FileSystemObject. If you are unfamiliar with theFileSystemObject, I strongly suggest you read the following articlesfirst: Reading/Writing Text Files Text Files & ASP our help system, we will have just one ASP page, help.asp. This filewill be responsible for displaying our help index file and the currenthelp topic. The help index file is a text file that lists all of the helptopics and the file which contains that help topic. Here is an example ofthe help index file: HelpIndex.txt:----------------Using the Response Objectresponse.txtUsing the Request Objectrequest.txtImpressing your Friends with ASPimpress.txtOpening Text Files with ASPtextfiles.txtThe help index file is broken down into a number of two-lined entries. Thefirst line is the title for the specific help file (i.e. Using theResponse Object) and the second line immediately following the title isthe name of the file that houses the help itself! For example, here's thecontents of response.txt: response.txt:--------------The Response Object:
The Response Object is ASP's way of communicating with the HTML stream.If you want to send data to the HTML stream, you need to issue aResponse.Write statement, such as:

Response.Write "Hello, World!"

Pretty easy, eh!? For each help topic you wanted to have, you would justneed to create a text or html file similar to the one above. Save the filein the same directory as the help.asp and the other help topic files, and,lastly, add the title/URL to the help index file. Once you've completedthose three steps, you're done!!! It's that easy to add new help topics!If you want to delete a help topic, simply remove its title/URL from thehelp index file. To modify an existing help topic, just edit the helptopic file (for example, if we wanted to add more content to the Responsehelp topic, we'd simply edit response.txt. It's time to bring it all together. We're going to do this by using asingle file, help.asp. Please keep in mind that all these files need to bein the same directory for help.asp to work as is.(The help.asp file can be viewed in the article at: in text format at: too difficult here. We use the QueryString to determine what helptopic the user is currently wanting to view. We then open the appropriatefile and display it! That's all there is to it! You can add a standardheader and footer to the help topics by simply adding code where thecomments talk about the header and footer. Happy Programming! ************************************************************************ More articles below, but first a word from our sponsor!!! :)******************************************************************************************************************************************* Developers Can Now Deploy ASP Applications Anywhere! Halcyon Software will release this month an Active Server Frameworkthat gives developers the ability to deploy Active Server Pages (ASP)applications on any Web Server, Application Server, or Operating Systemplatform. Instant ASP (iASP) not only provides standard Microsoft ASP compatibility on non-Microsoft platforms, it extends basic ASPcapabilities to provide support for Java, JavaServer Pages (JSP), andCORBA components as well. Instant ASP is $495 SRLP per deployment server.Developer editions of iASP are free! *** ***-- Please support our sponsors, they make WebWeekly possible! --*************************************************************************************************************************************************************************************************************** OTHER ARTICLES*********************************************************************Building an Internet Database Drilldown Application, Part 1 series of articles, by Roger Drye, demonstrates how to create adatabase drilldown application using ASP and Access. In Part 1 of thisseries, Roger demonstrates how to first secure the site using a simpleauthentication method! *********************************************************************A Nifty Way to Create Connection Strings article demonstrates a nifty way to quickly generate a connectionstring. No longer do you have to worry if you might have forgotten animportant parameter. It's as easy as cutting and pasting! *********************************************************************Advanced FAQ Added! finished adding an Advanced FAQ section to the popular ASP FAQ. I'vealso added another couple database articles to the Databases FAQ. If youwould like to see a frequently asked question that's not already there,please let me know! *********************************************************************System DSN or DSN-less Connection? boasts a more efficient connection time: System DSNs or DSN-lessconnections? This article records my personal quest for an answer to thiselusive question!! It's excitement and action, rolled into one dynamo ofan article! *********************************************************************Win Free Books from WROX! now until 7/10 we will be raffling off TEN *free* copies of Wrox'sASP 2.0 Programmer's Reference. Drop on by to sign up for yourchance to win a free copy of this book! You can also check out to seewhat free stuff we're currently giving away! Have a great day!********************************************************************* Humor Section humor section has been consistently growing, and we recently wonan award for it! I know this is a little off-topic for the WebWeekly,so I don't want to go into it; just wanted to invite you all to checkout our collection of jokes, original humor, and spoof sites!*********************************************************************I hope you enjoyed this WebWeekly. If you have any ideas, comments,suggestions, rants, or raves, please let me know by replying to thisemail. If you are not familiar with, be sure tocheck out, to view our sitemap.Happy Programming!****************************************************************************************************************************************** To subscribe to WebWeekly, point your browser to: To unsubscribe from WebWeekly, reply to this email with the following subject: UNSUBSCRIBE WEBWEEKLY******************************************************************************************************************************************Thank you for subscribing to WebWeekly!If you know someone who could benefit from a wealth of web developmenttechnology, invite them to visit

Source Code
Option Explicit

Function ReadDir(FolderName)
	On Error Resume Next
	Dim objFSO
	Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
	'Make sure the specified folder exists
	If Not objFSO.FolderExists(FolderName) Then
		ReadDir = "Folder does not exist. <br><b>This can be changed to " & _
				"a default message or nothing.</b>"
		Exit Function
	End If

	Dim objFolder
	Set objFolder = objFSO.GetFolder(FolderName)

	Dim I : I = -1
	ReDim arrFile(20)
	Dim objFile
	For Each objFile In objFolder.Files
		If Right(objFile, 4) = ".txt" Then
  			I = I + 1
			if I > UBound(arrFile) then ReDim Preserve arrFile(I + 20)
			arrFile(I) = objFile.Path
		End If
	'Make sure arrFile is the right size
	Redim Preserve arrFile(I)

    If I = -1 Then
		ReadDir = "No text files in this directory. <br><b>This can be " & _
		          "changed to a default message or nothing.</b>"
		Exit Function
	End If

	Dim RanFile : RanFile = Int((UBound(arrFile) - 1 + 1) * Rnd + 1)
	Dim strOutput : strOutput = ""
	Dim strFileName : strFileName = arrFile(RanFile)
	If objFSO.FileExists(strFileName) Then
		Dim objTextStream
		Set objTextStream = objFSO.OpenTextFile(strFileName, 1)
			Do While Not objTextStream.AtEndOfStream
				strOutput = strOutput & objTextStream.ReadLine
		ReadDir = "File does not exist. <br><b>This can be changed to a " & _
		          "default message or nothing.</b>"
		Exit Function
	End If
    ReadDir = strOutput
 Set objTextStream = Nothing
 Set objFolder = Nothing
 Set objFile = Nothing
 Set objFSO = Nothing
End Function

<h1>Selecting a Random File from a Directory and Outputting its Contents Demo</h1>


[Return to the User Tip]