Microsoft KB Archive/247745: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "&" to "&")
m (Text replacement - """ to """)
 
Line 56: Line 56:
Use the following steps to create an include file for use with ASP containing the '''RandomImage()''' function. The '''RandomImage()''' function randomly returns the name of an image based on the contents in a folder, which is passed by name to the function, or based on the name of a default image if no images are found in the folder.
Use the following steps to create an include file for use with ASP containing the '''RandomImage()''' function. The '''RandomImage()''' function randomly returns the name of an image based on the contents in a folder, which is passed by name to the function, or based on the name of a default image if no images are found in the folder.
<ol>
<ol>
<li><p>Create a folder named &quot;Images&quot; in your Web site's root folder.Copy a few .gif or .jpg images into the Images folder.Save the following ASP page as &quot;RandomTest.asp&quot; in your Web site's root folder:</p>
<li><p>Create a folder named "Images" in your Web site's root folder.Copy a few .gif or .jpg images into the Images folder.Save the following ASP page as "RandomTest.asp" in your Web site's root folder:</p>
<pre class="codesample"><% @Language=&quot;VBScript&quot; %>
<pre class="codesample"><% @Language="VBScript" %>
<% Option Explicit %>
<% Option Explicit %>
<html>
<html>
<head>
<head>
<!--#include virtual=&quot;/includes/RandomImage.inc&quot;-->
<!--#include virtual="/includes/RandomImage.inc"-->
</head>
</head>
<body>
<body>
<div align=&quot;center&quot;><center>
<div align="center"><center>
<table border=&quot;1&quot;>
<table border="1">
   <tr>
   <tr>
     <td bgcolor=&quot;#ffffff&quot;>
     <td bgcolor="#ffffff">
       <img src=&quot;<%=RandomImage(&quot;/images/&quot;,&quot;/images/default.gif&quot;)%>&quot;>
       <img src="<%=RandomImage("/images/","/images/default.gif")%>">
     </td>
     </td>
   </tr>
   </tr>
Line 78: Line 78:
<p>'''Note''': The call to the '''RandomImage()''' function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.<br />
<p>'''Note''': The call to the '''RandomImage()''' function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.<br />
<br />
<br />
Create a folder named &quot;Includes&quot; in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
<pre class="codesample"><%
<pre class="codesample"><%
   Function RandomImage(strPath,strDefault)
   Function RandomImage(strPath,strDefault)
Line 90: Line 90:
     ' this constant has the names of valid image file name
     ' this constant has the names of valid image file name
     ' extensions and can be modified for more image types
     ' extensions and can be modified for more image types
     Const strValid = &quot;.gif.jpg.png&quot;
     Const strValid = ".gif.jpg.png"


     ' make sure we have a trailing slash in the path
     ' make sure we have a trailing slash in the path
Line 97: Line 97:
     strPhysical = Server.MapPath(strPath)
     strPhysical = Server.MapPath(strPath)
     ' get a File System Object
     ' get a File System Object
     Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
     ' create a folder object
     ' create a folder object
     Set objFolder = objFSO.GetFolder(strPhysical)
     Set objFolder = objFSO.GetFolder(strPhysical)
Line 128: Line 128:
                         </pre>
                         </pre>
<p>Browse the RandomTest.asp page; it should display a single image in a table.</p></li>
<p>Browse the RandomTest.asp page; it should display a single image in a table.</p></li>
<li><p>Copy a few .gif or .jpg images into the Images folder.Save the following ASP page as &quot;RandomTest.asp&quot; in your Web site's root folder:</p>
<li><p>Copy a few .gif or .jpg images into the Images folder.Save the following ASP page as "RandomTest.asp" in your Web site's root folder:</p>
<pre class="codesample"><% @Language=&quot;VBScript&quot; %>
<pre class="codesample"><% @Language="VBScript" %>
<% Option Explicit %>
<% Option Explicit %>
<html>
<html>
<head>
<head>
<!--#include virtual=&quot;/includes/RandomImage.inc&quot;-->
<!--#include virtual="/includes/RandomImage.inc"-->
</head>
</head>
<body>
<body>
<div align=&quot;center&quot;><center>
<div align="center"><center>
<table border=&quot;1&quot;>
<table border="1">
   <tr>
   <tr>
     <td bgcolor=&quot;#ffffff&quot;>
     <td bgcolor="#ffffff">
       <img src=&quot;<%=RandomImage(&quot;/images/&quot;,&quot;/images/default.gif&quot;)%>&quot;>
       <img src="<%=RandomImage("/images/","/images/default.gif")%>">
     </td>
     </td>
   </tr>
   </tr>
Line 150: Line 150:
<p>'''Note''': The call to the '''RandomImage()''' function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.<br />
<p>'''Note''': The call to the '''RandomImage()''' function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.<br />
<br />
<br />
Create a folder named &quot;Includes&quot; in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
<pre class="codesample"><%
<pre class="codesample"><%
   Function RandomImage(strPath,strDefault)
   Function RandomImage(strPath,strDefault)
Line 162: Line 162:
     ' this constant has the names of valid image file name
     ' this constant has the names of valid image file name
     ' extensions and can be modified for more image types
     ' extensions and can be modified for more image types
     Const strValid = &quot;.gif.jpg.png&quot;
     Const strValid = ".gif.jpg.png"


     ' make sure we have a trailing slash in the path
     ' make sure we have a trailing slash in the path
Line 169: Line 169:
     strPhysical = Server.MapPath(strPath)
     strPhysical = Server.MapPath(strPath)
     ' get a File System Object
     ' get a File System Object
     Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
     ' create a folder object
     ' create a folder object
     Set objFolder = objFSO.GetFolder(strPhysical)
     Set objFolder = objFSO.GetFolder(strPhysical)
Line 200: Line 200:
                         </pre>
                         </pre>
<p>Browse the RandomTest.asp page; it should display a single image in a table.</p></li>
<p>Browse the RandomTest.asp page; it should display a single image in a table.</p></li>
<li><p>Save the following ASP page as &quot;RandomTest.asp&quot; in your Web site's root folder:</p>
<li><p>Save the following ASP page as "RandomTest.asp" in your Web site's root folder:</p>
<pre class="codesample"><% @Language=&quot;VBScript&quot; %>
<pre class="codesample"><% @Language="VBScript" %>
<% Option Explicit %>
<% Option Explicit %>
<html>
<html>
<head>
<head>
<!--#include virtual=&quot;/includes/RandomImage.inc&quot;-->
<!--#include virtual="/includes/RandomImage.inc"-->
</head>
</head>
<body>
<body>
<div align=&quot;center&quot;><center>
<div align="center"><center>
<table border=&quot;1&quot;>
<table border="1">
   <tr>
   <tr>
     <td bgcolor=&quot;#ffffff&quot;>
     <td bgcolor="#ffffff">
       <img src=&quot;<%=RandomImage(&quot;/images/&quot;,&quot;/images/default.gif&quot;)%>&quot;>
       <img src="<%=RandomImage("/images/","/images/default.gif")%>">
     </td>
     </td>
   </tr>
   </tr>
Line 222: Line 222:
<p>'''Note''': The call to the '''RandomImage()''' function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.<br />
<p>'''Note''': The call to the '''RandomImage()''' function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.<br />
<br />
<br />
Create a folder named &quot;Includes&quot; in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
<pre class="codesample"><%
<pre class="codesample"><%
   Function RandomImage(strPath,strDefault)
   Function RandomImage(strPath,strDefault)
Line 234: Line 234:
     ' this constant has the names of valid image file name
     ' this constant has the names of valid image file name
     ' extensions and can be modified for more image types
     ' extensions and can be modified for more image types
     Const strValid = &quot;.gif.jpg.png&quot;
     Const strValid = ".gif.jpg.png"


     ' make sure we have a trailing slash in the path
     ' make sure we have a trailing slash in the path
Line 241: Line 241:
     strPhysical = Server.MapPath(strPath)
     strPhysical = Server.MapPath(strPath)
     ' get a File System Object
     ' get a File System Object
     Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
     ' create a folder object
     ' create a folder object
     Set objFolder = objFSO.GetFolder(strPhysical)
     Set objFolder = objFSO.GetFolder(strPhysical)
Line 272: Line 272:
                         </pre>
                         </pre>
<p>Browse the RandomTest.asp page; it should display a single image in a table.</p></li>
<p>Browse the RandomTest.asp page; it should display a single image in a table.</p></li>
<li><p>Create a folder named &quot;Includes&quot; in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
<li><p>Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:</p>
<pre class="codesample"><%
<pre class="codesample"><%
   Function RandomImage(strPath,strDefault)
   Function RandomImage(strPath,strDefault)
Line 284: Line 284:
     ' this constant has the names of valid image file name
     ' this constant has the names of valid image file name
     ' extensions and can be modified for more image types
     ' extensions and can be modified for more image types
     Const strValid = &quot;.gif.jpg.png&quot;
     Const strValid = ".gif.jpg.png"


     ' make sure we have a trailing slash in the path
     ' make sure we have a trailing slash in the path
Line 291: Line 291:
     strPhysical = Server.MapPath(strPath)
     strPhysical = Server.MapPath(strPath)
     ' get a File System Object
     ' get a File System Object
     Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
     ' create a folder object
     ' create a folder object
     Set objFolder = objFSO.GetFolder(strPhysical)
     Set objFolder = objFSO.GetFolder(strPhysical)
Line 334: Line 334:
     ' this constant has the names of valid image file name
     ' this constant has the names of valid image file name
     ' extensions and can be modified for more image types
     ' extensions and can be modified for more image types
     Const strValid = &quot;.gif.jpg.png&quot;
     Const strValid = ".gif.jpg.png"


     ' make sure we have a trailing slash in the path
     ' make sure we have a trailing slash in the path
Line 341: Line 341:
     strPhysical = Server.MapPath(strPath)
     strPhysical = Server.MapPath(strPath)
     ' get a File System Object
     ' get a File System Object
     Set objFSO = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
     ' create a folder object
     ' create a folder object
     Set objFolder = objFSO.GetFolder(strPhysical)
     Set objFolder = objFSO.GetFolder(strPhysical)

Latest revision as of 13:50, 21 July 2020

Knowledge Base


Article ID: 247745

Article Last Modified on 11/21/2006



APPLIES TO

  • Microsoft Internet Information Services 5.0



This article was previously published under Q247745

SUMMARY

This article describes the process for returning a random image from a folder in your Web site using Microsoft Active Server Pages (ASP) and the Scripting.FileSystemObject.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

Random Images from ASP

Use the following steps to create an include file for use with ASP containing the RandomImage() function. The RandomImage() function randomly returns the name of an image based on the contents in a folder, which is passed by name to the function, or based on the name of a default image if no images are found in the folder.

  1. Create a folder named "Images" in your Web site's root folder.Copy a few .gif or .jpg images into the Images folder.Save the following ASP page as "RandomTest.asp" in your Web site's root folder:

    <% @Language="VBScript" %>
    <% Option Explicit %>
    <html>
    <head>
    <!--#include virtual="/includes/RandomImage.inc"-->
    </head>
    <body>
    <div align="center"><center>
    <table border="1">
      <tr>
        <td bgcolor="#ffffff">
          <img src="<%=RandomImage("/images/","/images/default.gif")%>">
        </td>
      </tr>
    </table>
    </center></div>
    </body>
    </html>
                            

    Note: The call to the RandomImage() function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.

    Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:

    <%
      Function RandomImage(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strImages, strPhysical, strFile
    
        ' this constant has the names of valid image file name
        ' extensions and can be modified for more image types
        Const strValid = ".gif.jpg.png"
    
        ' make sure we have a trailing slash in the path
        If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
        ' get the physical path of the folder
        strPhysical = Server.MapPath(strPath)
        ' get a File System Object
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        ' create a folder object
        Set objFolder = objFSO.GetFolder(strPhysical)
        ' get the files collection
        Set objFiles = objFolder.Files
    
        ' enumerate the files collection looking for images
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add vaild images to a string of image names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the image names into an array
        strImages = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strImages) > 1 Then
          ' get a random name
          RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
        Else
          ' otherwise return the default
          RandomImage = strDefault
        End If
    
      End Function
    %>
                            

    Browse the RandomTest.asp page; it should display a single image in a table.

  2. Copy a few .gif or .jpg images into the Images folder.Save the following ASP page as "RandomTest.asp" in your Web site's root folder:

    <% @Language="VBScript" %>
    <% Option Explicit %>
    <html>
    <head>
    <!--#include virtual="/includes/RandomImage.inc"-->
    </head>
    <body>
    <div align="center"><center>
    <table border="1">
      <tr>
        <td bgcolor="#ffffff">
          <img src="<%=RandomImage("/images/","/images/default.gif")%>">
        </td>
      </tr>
    </table>
    </center></div>
    </body>
    </html>
                            

    Note: The call to the RandomImage() function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.

    Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:

    <%
      Function RandomImage(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strImages, strPhysical, strFile
    
        ' this constant has the names of valid image file name
        ' extensions and can be modified for more image types
        Const strValid = ".gif.jpg.png"
    
        ' make sure we have a trailing slash in the path
        If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
        ' get the physical path of the folder
        strPhysical = Server.MapPath(strPath)
        ' get a File System Object
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        ' create a folder object
        Set objFolder = objFSO.GetFolder(strPhysical)
        ' get the files collection
        Set objFiles = objFolder.Files
    
        ' enumerate the files collection looking for images
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add vaild images to a string of image names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the image names into an array
        strImages = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strImages) > 1 Then
          ' get a random name
          RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
        Else
          ' otherwise return the default
          RandomImage = strDefault
        End If
    
      End Function
    %>
                            

    Browse the RandomTest.asp page; it should display a single image in a table.

  3. Save the following ASP page as "RandomTest.asp" in your Web site's root folder:

    <% @Language="VBScript" %>
    <% Option Explicit %>
    <html>
    <head>
    <!--#include virtual="/includes/RandomImage.inc"-->
    </head>
    <body>
    <div align="center"><center>
    <table border="1">
      <tr>
        <td bgcolor="#ffffff">
          <img src="<%=RandomImage("/images/","/images/default.gif")%>">
        </td>
      </tr>
    </table>
    </center></div>
    </body>
    </html>
                            

    Note: The call to the RandomImage() function passes the name of the default image as /Images/Default.gif. You will need to change that to a valid image name and path when you want the function to a return a default image when a folder contains no images.

    Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:

    <%
      Function RandomImage(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strImages, strPhysical, strFile
    
        ' this constant has the names of valid image file name
        ' extensions and can be modified for more image types
        Const strValid = ".gif.jpg.png"
    
        ' make sure we have a trailing slash in the path
        If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
        ' get the physical path of the folder
        strPhysical = Server.MapPath(strPath)
        ' get a File System Object
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        ' create a folder object
        Set objFolder = objFSO.GetFolder(strPhysical)
        ' get the files collection
        Set objFiles = objFolder.Files
    
        ' enumerate the files collection looking for images
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add vaild images to a string of image names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the image names into an array
        strImages = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strImages) > 1 Then
          ' get a random name
          RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
        Else
          ' otherwise return the default
          RandomImage = strDefault
        End If
    
      End Function
    %>
                            

    Browse the RandomTest.asp page; it should display a single image in a table.

  4. Create a folder named "Includes" in your Web site's root directory.Save the following ASP code in the Includes folder as RandomImage.inc:

    <%
      Function RandomImage(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strImages, strPhysical, strFile
    
        ' this constant has the names of valid image file name
        ' extensions and can be modified for more image types
        Const strValid = ".gif.jpg.png"
    
        ' make sure we have a trailing slash in the path
        If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
        ' get the physical path of the folder
        strPhysical = Server.MapPath(strPath)
        ' get a File System Object
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        ' create a folder object
        Set objFolder = objFSO.GetFolder(strPhysical)
        ' get the files collection
        Set objFiles = objFolder.Files
    
        ' enumerate the files collection looking for images
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add vaild images to a string of image names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the image names into an array
        strImages = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strImages) > 1 Then
          ' get a random name
          RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
        Else
          ' otherwise return the default
          RandomImage = strDefault
        End If
    
      End Function
    %>
                            

    Browse the RandomTest.asp page; it should display a single image in a table.

  5. Save the following ASP code in the Includes folder as RandomImage.inc:

    <%
      Function RandomImage(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strImages, strPhysical, strFile
    
        ' this constant has the names of valid image file name
        ' extensions and can be modified for more image types
        Const strValid = ".gif.jpg.png"
    
        ' make sure we have a trailing slash in the path
        If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
        ' get the physical path of the folder
        strPhysical = Server.MapPath(strPath)
        ' get a File System Object
        Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
        ' create a folder object
        Set objFolder = objFSO.GetFolder(strPhysical)
        ' get the files collection
        Set objFiles = objFolder.Files
    
        ' enumerate the files collection looking for images
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add vaild images to a string of image names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the image names into an array
        strImages = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strImages) > 1 Then
          ' get a random name
          RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
        Else
          ' otherwise return the default
          RandomImage = strDefault
        End If
    
      End Function
    %>
                            

    Browse the RandomTest.asp page; it should display a single image in a table.

  6. Browse the RandomTest.asp page; it should display a single image in a table.


Keywords: kbhowto kbscript kbcodesnippet kbfso KB247745