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!

Access '97 Authentication using ASP
by Gary Van Sluis

* This article discusses how to authenticate users through ASP when usingAccess as your backend.

If you have been working with Asp and Access 97 you have probably comeacross the question of using Access's authentication either alone or incombination with NT. And if you havenít come across that specific question,you just did. Well Access has a separate authentication method from windows.What I mean by separate is that with Windows NT you have two choices to usewhen interfacing with the web, Basic and NT Challenge and Response. SQLServer can take advantage of NT security but Access can not. Also you mighthave a tad bit of work when planning on Netscape users using basic andExplorer users using NTCR. (Netscape can not use NTCR) Humm.... So what doesall this mean if you are using Access and ASP? Well it means that you canuse the built in security that comes with Access, you can have your webpages authenticate usernames and passwords against the database, and it willbe browser independent! So everyone can use it.So what will you need?

  • Article1.htm is first page
  • Article2.asp is secondpage
  • Badlogin.htm is result of bad login.

    Otherwise info is displayed on article2

    Happy Programming!

    To subscribe to WebDaily, point your browser to:

    Please forward this information to a friend, including thesubscription URL! 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
    	'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]