A lot of people use arrays or recordsets to pick a random file from a folder. Here is a slightly more efficient way... instead of loading all of the filenames into a large array, just run through the list until you hit the random number.
<% Set fso = CreateObject("Scripting.FileSystemObject") Set fold = fso.GetFolder(Server.MapPath("folderName")) Set fileset = fold.files fileCount = fileset.count if fileCount > 0 then counter = 1 randomize fileToPick = clng((rnd * fileCount) + 0.5) for each file in fileset if counter < fileToPick then counter = counter + 1 else randomFile = file.name exit for end if next response.write randomFile else response.write "Empty folder." end if set fileset = nothing set fold = nothing set fso = nothing %> |