May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.
<% 'More complex function that requires use of server functions and means any image can be dropped into 'the RandomImage folder. It must be a gif or jpg, although a parameter can be added into the function 'to deal with other image types. Function RandomImage(ImagesFolderPath, ImageFileTypes, ImageDescription) 'Declare variables Dim CompleteImagesFolderPath Dim FileSystemObject Dim ImageFolder Dim Files Dim i Dim ImageFiles Dim File Dim FileName Dim FileExtension Dim RandomNumber 'Find the complete path to image folder by using Server.MapPath CompleteImagesFolderPath = Server.MapPath(ImagesFolderPath) 'Create an instance of the FileSystemObject which allows ASP to 'access the file system Set FileSystemObject = Server.CreateObject("Scripting.FileSystemObject") 'Check that the folder containing the images exists If Not FileSystemObject.FolderExists(CompleteImagesFolderPath) Then RandomImage = "Error 0: Cannot find requested folder" Set FileSystemObject = nothing Exit Function End If 'Get the folder containing the images Set ImageFolder = FileSystemObject.GetFolder(CompleteImagesFolderPath) 'Get a list of all the files within the images folder Set Files = ImageFolder.Files 'Use a dictionary object to temporarily store the image file names i = 1 Set ImageFiles = Server.CreateObject("Scripting.Dictionary") 'Loop through the list of files within the images folder. 'If the file has a file extension that is in the list of 'file types specified in the ImageFileTypes function parameter, 'then add the file name to the ImageFiles dictionary object For Each File in Files FileName = File.Name FileExtension = Right(FileName, Len(FileName) - (InStrRev(FileName, "."))) If InStr(1,ImageFileTypes,FileExtension,vbTextCompare) > 0 then ImageFiles.Add i, FileName i = i + 1 End If Next 'Destroy objects that are no longer required Set ImageFolder = nothing Set Files = nothing Set FileSystemObject = nothing 'Initialise the random number generator Randomize ' Check that image file(s) have been found If ImageFiles.Count = 0 Then RandomImage = "Error 1: Requested folder does not contain any image files" Exit Function End If 'Generate a random number between 1 and the number of image files RandomNumber = Int((ImageFiles.Count) * Rnd + 1) 'Return a hyperlink to a random image file RandomImage = "<img src=" & Chr(34) & ImagesFolderPath & ImageFiles.Item(RandomNumber) & Chr(34) & " alt=" & Chr(34) & ImageDescription & Chr(34) & "width='150px' height='150px'>" 'This function will strip out the filename and use it as a Caption for the picture Dim Caption Caption = ImageFiles.Item(RandomNumber) parts = split(Caption, ".") file_name = parts(0) ext = parts(1) Caption = "<div class='caption'>" & file_name & "</div>" Response.Write(Caption) Set ImageFiles = nothing End Function 'Call the function and write to the screen, passing in the arguments for the function. Response.Write RandomImage("RandomImage/", "gif jpg", "Plasmocom") 'The function has three arguments. The first (ImagesFolderPath) is 'the name of the folder containing the images in this case Random Image, 'and it should be a sub-folder of the folder that contains the ASP document. 'The second argument should be a string containing a list of file types to use 'as images (e.g. jpeg, jpg, gif). Finally, the last argument is a 'short sentence to describe your image (which is used as the images ALT tag). %>
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
|ASP and Looping Thru a XML Document||4||45|
|Login Script to pull users IP Phone from AD and then create a registry key in HKCU with that value||8||31|
|how to modify the VBScript so that it plays sound via Shell command default available player.||7||66|
|How to set PAUSE for this script ?||5||22|
Join the community of 500,000 technology professionals and ask your questions.