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.

WebDaily: Your daily source for Web Technology Tips and Tricks! -Forward this email and subscription information to a fellow developer! ---- Need Web Technology Articles and Tutorials? ---- Visit********************************************************************** Hooking Functions in JavaScript*********************************************************************** This article discusses how to hook functions using JavaScript.As you probably already know, Internet Explorer and Netscape Navigator areboth event-driven browsers. Therefore, when you write scripting code ineither JavaScript or VBScript, you often code for events. For example,you could write a FORM element like so: Internet Explorer even allows you to write your VBScript event-drivenfunctions just like you would using VisualBASIC. For example, the aboveexample could be coded as follows: JavaScript code can also be written in this event-handling fashion.While many ASP developers are quite use to and comfortable with VBScript,client-side script _MUST_ really be written in JavaScript, so not toexclude the Netscape fans out there. Therefore, learning and usingJavaScript is important for every web developer.In JavaScript, you "hook" a function by assigning an element's event to aparticular function. Let's look at an example:

Note that the function name being hooked doesn't have any parenthesisaround it. That is because we are after the function pointer, we are notinterested in calling the function, which the parenthesis do. If you areinterested in learning more about function pointers or pointers ingeneral, I invite you to visit oremail me with a question.Now, whenever you click the button a little modal messagebox will pop upinforming you that you've clicked the button. Note that the SCRIPT blockwhich hooks the function to the given event needs to appear after thelocation in the HTML where the element has been defined.We are not limited to hooking our functions to buttons in FORMs. SinceFRAMEs and WINDOWs are considered HTML objects, we can hook our functionsto events which occur inside both FRAMEs and WINDOWs. For example, we canopen a window, and have events which occur in that window triggerJavaScript functions written in our page which creates the window! Hereis an example: Now whenever the body of the window created is clicked, our ClickedWindowfunction will fire. You can also apply the same logic to handle events inother frames. Hooking functions is a powerful capability of JavaScript,allowing the developer to essentially redirect an event's function pointerto a function pointer of a function the developer wrote himself! (Trysaying that five times fast!) In other words, a web developer can writehis own event-handling functions and hook them to any event that isexposed. Pretty neat. For more JavaScript information, be sure to visit Programming!****************************************************************************************************************************************** To subscribe to WebDaily, point your browser to: To unsubscribe from WebDaily, reply to this email with the following subject: UNSUBSCRIBE WEBDAILY******************************************************************************************************************************************Thank you for subscribing to WebDaily!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]