Random Image Display
Here's a small code snippet that displays a random image.
Every time a page containing a call of this subroutine is
hit, an image from the input folder is displayed randomly.
Sub DisplayRandomImage( szImageFolder )
Dim szImagesFolderPath, szImageTag
Dim nRandom, nCounter, nFilesCount
Dim oFS, oFolder, oFileCollection, oFile
If Right(szImageFolder, 1) <> "/" Then szImageFolder = szImageFolder & "/"
' translate virtual folder into physical path
szImagesFolderPath = Server.MapPath (szImageFolder)
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder(szImagesFolderPath)
Set oFileCollection = oFolder.Files
nFilesCount = oFileCollection.Count
if nFilesCount = 0 then
Response.Write "No image available" ' no image in this folder
Exit Sub
end if
Randomize(Cbyte(Left(Right(Time(),5),2)))
nRandom = int(nFilesCount * rnd) + 1
' in Visual Basic you could directly access the member of a collection like this:
' oFileCollection.Item(nRandom)
' but in ASP you have to loop through the collection:
nCounter = 0
For Each oFile in oFileCollection
nCounter = nCounter + 1
if nCounter = nRandom then
szImageTag = "<img src=""" & szImageFolder & oFile.Name & """>"
Response.Write szImageTag
Exit For
end if
Next
Set oFileCollection = Nothing
Set oFolder = Nothing
Set oFS = Nothing
End Sub
Submitted by Heinz Duschanek