<% 'Declare Variables dim fsoFolder, FolderRoot, FolderSubs, RndFolderNum, objFolder, intFileLooper dim objFolderToUse, objFolderToUsePath, fsoFile, objFolderObject, objFileCollection dim intFileNumberToUse, objFile, objImageFileToUse, strImageSrcText 'This is a relative path to your image gallery root Const ImagesRoot = "images/animaux" ' Get List of Subfolders under ImagesRoot Set fsoFolder = CreateObject("Scripting.FileSystemObject") Set FolderRoot = fsoFolder.GetFolder(Server.MapPath(ImagesRoot)) Set FolderSubs = FolderRoot.subfolders ' Get a Random Folder Number from List randomize RndFolderNum = Int(FolderSubs.count * Rnd) ' Associate Folder name based on Random Number Results For Each objFolder in FolderSubs If intFileLooper = RndFolderNum Then ' Get a "handle" on the appropriate file Set objFolderToUse = objFolder Exit For End If intFileLooper = intFileLooper + 1 Next 'Clean up Above Variables Set fsoFolder = Nothing Set FolderRoot = Nothing Set FolderSubs = Nothing Set RndFolderNum = Nothing ' Build Complete Virtual Path to Random Folder objFolderToUsePath = (ImagesRoot & "/" & objFolderToUse.name & "/") ' Get a List of Image Files from Folders Set fsoFile = Server.CreateObject("Scripting.FileSystemObject") Set objFolderObject = fsoFile.GetFolder(Server.MapPath(objFolderToUsePath)) Set objFileCollection = objFolderObject.Files ' Get a Random File Number from List randomize intFileNumberToUse = Int(objFileCollection.Count * Rnd) + 1 ' Associate File name based on Random Number Results intFileLooper = 1 For Each objFile in objFileCollection If intFileLooper = intFileNumberToUse Then ' Get a "handle" on the appropriate file Set objImageFileToUse = objFile Exit For End If intFileLooper = intFileLooper + 1 Next ' Clean up Above Variables Set fsoFile = Nothing Set objFolderObject = Nothing Set objFileCollection = Nothing ' Build our img src tag text (Note this is a full physical path, not virtual) strImageSrcText = objImageFileToUse %>
<% 'Clean up Remaining Variables Set objFolderToUse = Nothing Set objFolderToUsePath = Nothing Set objImageFileToUse = Nothing Set strImageSrcText = Nothing %>