Display a random file using ASP

by Carlos Baez
Thursday, 28th July 2005

When doing this web site I wanted to display certain random ads like the book selection that appears always in the right hand bar.

Every ad has its own file which contains the HTML code for the ad (image, description and a link). To display a random ad I use an include file that opens a random file and displays its content. The text files have the following format: number.txt (1.txt, 2.txt,...)

This is the include file used:

<%
Dim DispFile
Dim FileContent
Randomize
DispFile = Int(Rnd*10)+1 ' Number of files (where 10 is the total number of files)
Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
Set Instream = FileObject.OpenTextFile ("e:/inetpub/quadcomm/books/"&DispFile&".txt", 1, FALSE, FALSE)
FileContent = Instream.ReadAll
Response.Write (FileContent)
%>

The last step is to include the file wherever you want to display the file:

<!--#include file="randomfile.asp"-->

I hope you find this useful!


© Carlos Baez
View the original article Display a random file using ASP