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!********************************************************************* Useful SQL Scripts*********************************************************************As dynamic web site developers, it is our job to be able to returnrequested information from a database to a user. Using SQL there is analmost limitless amount of possible reports which can be generated. Thereare some often needed functions from SQL which can be a bit tricky toimplement. In this article, I intend to discuss some of those useful buttricky scripts.SCRIPT 1: Finding Rows with Matching Columns=============================================Let's say that we have a table which keeps track of projects our employeesare currently working on. This table will have three rows: EmployeeID int PK PreferredProjectID int FK CurrentProjectID int FKPreferredProjectID and CurrentProjectID are foreign keys to a Projecttable, which has all of our current and expected projects stored. So thistable not only keeps track of what project an employee is working on, butwhat project he or she wants to be working on. There are instances(hopefully several of them) where an employee is working on the project heor she wants to be working on. When this is the case, PreferredProjectIDwill equal CurrentProjectID. Let's look at a script which will return usa list of EmployeeIDs that are working on the project they are interestedin: SELECT I1.EmployeeID FROM EmployeeProjects I1 WHERE EXISTS (SELECT I2.PreferredProjectID, I2.CurrentProjectID FROM EmployeeProjects I2 WHERE I2.PreferredProjectID = I1.PreferredProjectID AND I2.CurrentProjectID = I1.CurrentProjectID GROUP BY I2.CurrentProjectID, I2.PreferredProjectID HAVING Count(*) > 1)This subquery uses a correlated subquery to return all of the rows whichhave a matching PreferredProjectID and a matching CurrentProjectID.SCRIPT 2: Getting the Highest Ranked Values============================================Let's say that you have a table consisting of total revenue for yourcompany for given months of the year. If you wanted to view the top 3months for 1997, you can do so very easily with the TOP keyword. You canalso view the top n percent. Here is a script which will show you the top3 months: SELECT TOP 3 Month,Revenue FROM MonthlyRevenue WHERE Year = 1997 ORDER BY RevenueThe ORDER BY clause is essentail when using TOP, so it knows what field toreturn the TOP values for. You could also get the same dataset returnedby using the script: SELECT TOP 25 PERCENT Month,Revenue FROM MonthlyRevenue WHERE Year = 1997 ORDER BY RevenueI hope you found these scripts useful. If you have any questions or anyscripts you'd like to share with WebDaily, please email them toinfo@4guysfromrolla.comHappy Programming!!****************************************************************************************************************************************** To subscribe to WebDaily, point your browser to: http://www.4GuysFromRolla.com/webtech/webdaily To unsubscribe from WebDaily, reply to this email with the following subject: UNSUBSCRIBE WEBDAILY******************************************************************************************************************************************Thank you for subscribing to WebDaily! Please forward this email to afellow web developer! Happy Programming!


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

	Randomize
	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
			Loop
		objTextStream.Close
	Else
		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>
...

<%
  Response.Write(ReadDir(Server.MapPath("/webtech/WebDaily")))
%>


[Return to the User Tip]