Difference between revisions of "Microsoft KB Archive/248027"

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
 
(3 intermediate revisions by the same user not shown)
Line 68: Line 68:
 
Use the following steps to create an include file for use with ASP containing a function named '''RandomSound()'''. This function randomly returns the name of a sound based on the contents in a folder, which is passed by name to the function, or the name of a default sound if no sound files are found in the folder.
 
Use the following steps to create an include file for use with ASP containing a function named '''RandomSound()'''. This function randomly returns the name of a sound based on the contents in a folder, which is passed by name to the function, or the name of a default sound if no sound files are found in the folder.
 
<ol>
 
<ol>
<li><p>Create a folder named &quot;Sounds&quot; in your Web site's root folder.<br />
+
<li><p>Create a folder named "Sounds" in your Web site's root folder.<br />
 
<br />
 
<br />
 
Copy a few .wav, .mid, or .rmi sound files into the Sounds folder. (There are usually several in the %WINDIR%\Media folder.)<br />
 
Copy a few .wav, .mid, or .rmi sound files into the Sounds folder. (There are usually several in the %WINDIR%\Media folder.)<br />
 
<br />
 
<br />
Save the following ASP page as &quot;RandomTest.asp&quot; in your Web site's root folder:</p>
+
Save the following ASP page as "RandomTest.asp" in your Web site's root folder:</p>
<pre class="codesample">&lt;% @Language=&quot;VBScript&quot; %&gt;
+
<pre class="codesample"><% @Language="VBScript" %>
&lt;% Option Explicit %&gt;
+
<% Option Explicit %>
&lt;html&gt;
+
<html>
&lt;head&gt;
+
<head>
&lt;!--#include virtual=&quot;/includes/RandomSound.inc&quot;--&gt;
+
<!--#include virtual="/includes/RandomSound.inc"-->
&lt;bgsound src=&quot;&lt;%=RandomSound(&quot;/sounds/&quot;,&quot;/sounds/default.mid&quot;)%&gt;&quot;&gt;
+
<bgsound src="<%=RandomSound("/sounds/","/sounds/default.mid")%>">
&lt;/head&gt;
+
</head>
&lt;body&gt;
+
<body>
&lt;h2 align=&quot;center&quot;&gt;Random Background Sound Test&lt;/h2&gt;
+
<h2 align="center">Random Background Sound Test</h2>
&lt;p&gt;Hit &quot;Refresh&quot; in your browser to change sounds.&lt;/p&gt;
+
<p>Hit "Refresh" in your browser to change sounds.</p>
&lt;/body&gt;
+
</body>
&lt;/html&gt;
+
</html>
 
                         </pre>
 
                         </pre>
 
<p>'''Note''': The call to the '''RandomSound()''' function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.<br />
 
<p>'''Note''': The call to the '''RandomSound()''' function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.<br />
 
<br />
 
<br />
Create a folder named &quot;Includes&quot; in your Web site's root directory.<br />
+
Create a folder named "Includes" in your Web site's root directory.<br />
 
<br />
 
<br />
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
<pre class="codesample">&lt;%
+
<pre class="codesample"><%
 
   Function RandomSound(strPath,strDefault)
 
   Function RandomSound(strPath,strDefault)
 
     On Error Resume Next
 
     On Error Resume Next
Line 102: Line 102:
 
     ' this constant has the names of valid file extensions
 
     ' this constant has the names of valid file extensions
 
     ' it can be customized for more or less file types
 
     ' it can be customized for more or less file types
     Const strValid = &quot;.wav.mid.rmi&quot;
+
     Const strValid = ".wav.mid.rmi"
  
 
     ' make sure we have a trailing slash in the path
 
     ' make sure we have a trailing slash in the path
     If Right(strPath,1) &lt;&gt; Chr(47) Then strPath = strPath &amp; Chr(47)
+
     If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
 
     ' get the physical path of the folder
 
     ' get the physical path of the folder
 
     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 120: Line 120:
 
       If Instr(strValid,Right(strFile,4)) Then
 
       If Instr(strValid,Right(strFile,4)) Then
 
         ' add valid files to a string of file names
 
         ' add valid files to a string of file names
         strFiles = strFiles &amp; strFile &amp; vbTab
+
         strFiles = strFiles & strFile & vbTab
 
       End If
 
       End If
 
     Next
 
     Next
Line 128: Line 128:
 
      
 
      
 
     ' if we have an array...
 
     ' if we have an array...
     If UBound(strSound) &gt; 1 Then
+
     If UBound(strSound) > 1 Then
 
       ' get a random name
 
       ' get a random name
       RandomSound = strPath &amp; strSound(Int(Rnd(1)*UBound(strSound)))
+
       RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
 
     Else
 
     Else
 
       ' otherwise return the default
 
       ' otherwise return the default
Line 137: Line 137:
  
 
   End Function
 
   End Function
%&gt;
+
%>
 
                         </pre>
 
                         </pre>
 
<p><br />
 
<p><br />
Line 144: Line 144:
 
<li><p>Copy a few .wav, .mid, or .rmi sound files into the Sounds folder. (There are usually several in the %WINDIR%\Media folder.)<br />
 
<li><p>Copy a few .wav, .mid, or .rmi sound files into the Sounds folder. (There are usually several in the %WINDIR%\Media folder.)<br />
 
<br />
 
<br />
Save the following ASP page as &quot;RandomTest.asp&quot; in your Web site's root folder:</p>
+
Save the following ASP page as "RandomTest.asp" in your Web site's root folder:</p>
<pre class="codesample">&lt;% @Language=&quot;VBScript&quot; %&gt;
+
<pre class="codesample"><% @Language="VBScript" %>
&lt;% Option Explicit %&gt;
+
<% Option Explicit %>
&lt;html&gt;
+
<html>
&lt;head&gt;
+
<head>
&lt;!--#include virtual=&quot;/includes/RandomSound.inc&quot;--&gt;
+
<!--#include virtual="/includes/RandomSound.inc"-->
&lt;bgsound src=&quot;&lt;%=RandomSound(&quot;/sounds/&quot;,&quot;/sounds/default.mid&quot;)%&gt;&quot;&gt;
+
<bgsound src="<%=RandomSound("/sounds/","/sounds/default.mid")%>">
&lt;/head&gt;
+
</head>
&lt;body&gt;
+
<body>
&lt;h2 align=&quot;center&quot;&gt;Random Background Sound Test&lt;/h2&gt;
+
<h2 align="center">Random Background Sound Test</h2>
&lt;p&gt;Hit &quot;Refresh&quot; in your browser to change sounds.&lt;/p&gt;
+
<p>Hit "Refresh" in your browser to change sounds.</p>
&lt;/body&gt;
+
</body>
&lt;/html&gt;
+
</html>
 
                         </pre>
 
                         </pre>
 
<p>'''Note''': The call to the '''RandomSound()''' function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.<br />
 
<p>'''Note''': The call to the '''RandomSound()''' function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.<br />
 
<br />
 
<br />
Create a folder named &quot;Includes&quot; in your Web site's root directory.<br />
+
Create a folder named "Includes" in your Web site's root directory.<br />
 
<br />
 
<br />
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
<pre class="codesample">&lt;%
+
<pre class="codesample"><%
 
   Function RandomSound(strPath,strDefault)
 
   Function RandomSound(strPath,strDefault)
 
     On Error Resume Next
 
     On Error Resume Next
Line 174: Line 174:
 
     ' this constant has the names of valid file extensions
 
     ' this constant has the names of valid file extensions
 
     ' it can be customized for more or less file types
 
     ' it can be customized for more or less file types
     Const strValid = &quot;.wav.mid.rmi&quot;
+
     Const strValid = ".wav.mid.rmi"
  
 
     ' make sure we have a trailing slash in the path
 
     ' make sure we have a trailing slash in the path
     If Right(strPath,1) &lt;&gt; Chr(47) Then strPath = strPath &amp; Chr(47)
+
     If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
 
     ' get the physical path of the folder
 
     ' get the physical path of the folder
 
     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 192: Line 192:
 
       If Instr(strValid,Right(strFile,4)) Then
 
       If Instr(strValid,Right(strFile,4)) Then
 
         ' add valid files to a string of file names
 
         ' add valid files to a string of file names
         strFiles = strFiles &amp; strFile &amp; vbTab
+
         strFiles = strFiles & strFile & vbTab
 
       End If
 
       End If
 
     Next
 
     Next
Line 200: Line 200:
 
      
 
      
 
     ' if we have an array...
 
     ' if we have an array...
     If UBound(strSound) &gt; 1 Then
+
     If UBound(strSound) > 1 Then
 
       ' get a random name
 
       ' get a random name
       RandomSound = strPath &amp; strSound(Int(Rnd(1)*UBound(strSound)))
+
       RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
 
     Else
 
     Else
 
       ' otherwise return the default
 
       ' otherwise return the default
Line 209: Line 209:
  
 
   End Function
 
   End Function
%&gt;
+
%>
 
                         </pre>
 
                         </pre>
 
<p><br />
 
<p><br />
 
<br />
 
<br />
 
When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.</p></li>
 
When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.</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">&lt;% @Language=&quot;VBScript&quot; %&gt;
+
<pre class="codesample"><% @Language="VBScript" %>
&lt;% Option Explicit %&gt;
+
<% Option Explicit %>
&lt;html&gt;
+
<html>
&lt;head&gt;
+
<head>
&lt;!--#include virtual=&quot;/includes/RandomSound.inc&quot;--&gt;
+
<!--#include virtual="/includes/RandomSound.inc"-->
&lt;bgsound src=&quot;&lt;%=RandomSound(&quot;/sounds/&quot;,&quot;/sounds/default.mid&quot;)%&gt;&quot;&gt;
+
<bgsound src="<%=RandomSound("/sounds/","/sounds/default.mid")%>">
&lt;/head&gt;
+
</head>
&lt;body&gt;
+
<body>
&lt;h2 align=&quot;center&quot;&gt;Random Background Sound Test&lt;/h2&gt;
+
<h2 align="center">Random Background Sound Test</h2>
&lt;p&gt;Hit &quot;Refresh&quot; in your browser to change sounds.&lt;/p&gt;
+
<p>Hit "Refresh" in your browser to change sounds.</p>
&lt;/body&gt;
+
</body>
&lt;/html&gt;
+
</html>
 
                         </pre>
 
                         </pre>
 
<p>'''Note''': The call to the '''RandomSound()''' function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.<br />
 
<p>'''Note''': The call to the '''RandomSound()''' function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.<br />
 
<br />
 
<br />
Create a folder named &quot;Includes&quot; in your Web site's root directory.<br />
+
Create a folder named "Includes" in your Web site's root directory.<br />
 
<br />
 
<br />
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
<pre class="codesample">&lt;%
+
<pre class="codesample"><%
 
   Function RandomSound(strPath,strDefault)
 
   Function RandomSound(strPath,strDefault)
 
     On Error Resume Next
 
     On Error Resume Next
Line 244: Line 244:
 
     ' this constant has the names of valid file extensions
 
     ' this constant has the names of valid file extensions
 
     ' it can be customized for more or less file types
 
     ' it can be customized for more or less file types
     Const strValid = &quot;.wav.mid.rmi&quot;
+
     Const strValid = ".wav.mid.rmi"
  
 
     ' make sure we have a trailing slash in the path
 
     ' make sure we have a trailing slash in the path
     If Right(strPath,1) &lt;&gt; Chr(47) Then strPath = strPath &amp; Chr(47)
+
     If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
 
     ' get the physical path of the folder
 
     ' get the physical path of the folder
 
     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 262: Line 262:
 
       If Instr(strValid,Right(strFile,4)) Then
 
       If Instr(strValid,Right(strFile,4)) Then
 
         ' add valid files to a string of file names
 
         ' add valid files to a string of file names
         strFiles = strFiles &amp; strFile &amp; vbTab
+
         strFiles = strFiles & strFile & vbTab
 
       End If
 
       End If
 
     Next
 
     Next
Line 270: Line 270:
 
      
 
      
 
     ' if we have an array...
 
     ' if we have an array...
     If UBound(strSound) &gt; 1 Then
+
     If UBound(strSound) > 1 Then
 
       ' get a random name
 
       ' get a random name
       RandomSound = strPath &amp; strSound(Int(Rnd(1)*UBound(strSound)))
+
       RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
 
     Else
 
     Else
 
       ' otherwise return the default
 
       ' otherwise return the default
Line 279: Line 279:
  
 
   End Function
 
   End Function
%&gt;
+
%>
 
                         </pre>
 
                         </pre>
 
<p><br />
 
<p><br />
 
<br />
 
<br />
 
When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.</p></li>
 
When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.</p></li>
<li><p>Create a folder named &quot;Includes&quot; in your Web site's root directory.<br />
+
<li><p>Create a folder named "Includes" in your Web site's root directory.<br />
 
<br />
 
<br />
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
 
Save the following ASP code in the Includes folder as RandomSound.inc:</p>
<pre class="codesample">&lt;%
+
<pre class="codesample"><%
 
   Function RandomSound(strPath,strDefault)
 
   Function RandomSound(strPath,strDefault)
 
     On Error Resume Next
 
     On Error Resume Next
Line 298: Line 298:
 
     ' this constant has the names of valid file extensions
 
     ' this constant has the names of valid file extensions
 
     ' it can be customized for more or less file types
 
     ' it can be customized for more or less file types
     Const strValid = &quot;.wav.mid.rmi&quot;
+
     Const strValid = ".wav.mid.rmi"
  
 
     ' make sure we have a trailing slash in the path
 
     ' make sure we have a trailing slash in the path
     If Right(strPath,1) &lt;&gt; Chr(47) Then strPath = strPath &amp; Chr(47)
+
     If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
 
     ' get the physical path of the folder
 
     ' get the physical path of the folder
 
     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 316: Line 316:
 
       If Instr(strValid,Right(strFile,4)) Then
 
       If Instr(strValid,Right(strFile,4)) Then
 
         ' add valid files to a string of file names
 
         ' add valid files to a string of file names
         strFiles = strFiles &amp; strFile &amp; vbTab
+
         strFiles = strFiles & strFile & vbTab
 
       End If
 
       End If
 
     Next
 
     Next
Line 324: Line 324:
 
      
 
      
 
     ' if we have an array...
 
     ' if we have an array...
     If UBound(strSound) &gt; 1 Then
+
     If UBound(strSound) > 1 Then
 
       ' get a random name
 
       ' get a random name
       RandomSound = strPath &amp; strSound(Int(Rnd(1)*UBound(strSound)))
+
       RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
 
     Else
 
     Else
 
       ' otherwise return the default
 
       ' otherwise return the default
Line 333: Line 333:
  
 
   End Function
 
   End Function
%&gt;
+
%>
 
                         </pre>
 
                         </pre>
 
<p><br />
 
<p><br />
Line 339: Line 339:
 
When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.</p></li>
 
When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.</p></li>
 
<li><p>Save the following ASP code in the Includes folder as RandomSound.inc:</p>
 
<li><p>Save the following ASP code in the Includes folder as RandomSound.inc:</p>
<pre class="codesample">&lt;%
+
<pre class="codesample"><%
 
   Function RandomSound(strPath,strDefault)
 
   Function RandomSound(strPath,strDefault)
 
     On Error Resume Next
 
     On Error Resume Next
Line 350: Line 350:
 
     ' this constant has the names of valid file extensions
 
     ' this constant has the names of valid file extensions
 
     ' it can be customized for more or less file types
 
     ' it can be customized for more or less file types
     Const strValid = &quot;.wav.mid.rmi&quot;
+
     Const strValid = ".wav.mid.rmi"
  
 
     ' make sure we have a trailing slash in the path
 
     ' make sure we have a trailing slash in the path
     If Right(strPath,1) &lt;&gt; Chr(47) Then strPath = strPath &amp; Chr(47)
+
     If Right(strPath,1) <> Chr(47) Then strPath = strPath & Chr(47)
 
     ' get the physical path of the folder
 
     ' get the physical path of the folder
 
     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 368: Line 368:
 
       If Instr(strValid,Right(strFile,4)) Then
 
       If Instr(strValid,Right(strFile,4)) Then
 
         ' add valid files to a string of file names
 
         ' add valid files to a string of file names
         strFiles = strFiles &amp; strFile &amp; vbTab
+
         strFiles = strFiles & strFile & vbTab
 
       End If
 
       End If
 
     Next
 
     Next
Line 376: Line 376:
 
      
 
      
 
     ' if we have an array...
 
     ' if we have an array...
     If UBound(strSound) &gt; 1 Then
+
     If UBound(strSound) > 1 Then
 
       ' get a random name
 
       ' get a random name
       RandomSound = strPath &amp; strSound(Int(Rnd(1)*UBound(strSound)))
+
       RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
 
     Else
 
     Else
 
       ' otherwise return the default
 
       ' otherwise return the default
Line 385: Line 385:
  
 
   End Function
 
   End Function
%&gt;
+
%>
 
                         </pre>
 
                         </pre>
 
<p><br />
 
<p><br />

Latest revision as of 13:50, 21 July 2020

Knowledge Base


Article ID: 248027

Article Last Modified on 8/8/2007



APPLIES TO

  • Microsoft Internet Information Server 4.0
  • Microsoft Internet Information Services 5.0



This article was previously published under Q248027

We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 6.0 running on Microsoft Windows Server 2003. IIS 6.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site:

SUMMARY

This articles describes a process of returning a random background sound 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 Sounds from ASP

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

  1. Create a folder named "Sounds" in your Web site's root folder.

    Copy a few .wav, .mid, or .rmi sound files into the Sounds folder. (There are usually several in the %WINDIR%\Media 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/RandomSound.inc"-->
    <bgsound src="<%=RandomSound("/sounds/","/sounds/default.mid")%>">
    </head>
    <body>
    <h2 align="center">Random Background Sound Test</h2>
    <p>Hit "Refresh" in your browser to change sounds.</p>
    </body>
    </html>
                            

    Note: The call to the RandomSound() function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.

    Create a folder named "Includes" in your Web site's root directory.

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

    <%
      Function RandomSound(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strSound, strPhysical, strFile
    
        ' this constant has the names of valid file extensions
        ' it can be customized for more or less file types
        Const strValid = ".wav.mid.rmi"
    
        ' 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 valid files
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add valid files to a string of file names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the names into an array
        strSound = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strSound) > 1 Then
          ' get a random name
          RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
        Else
          ' otherwise return the default
          RandomSound = strDefault
        End If
    
      End Function
    %>
                            



    When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.

  2. Copy a few .wav, .mid, or .rmi sound files into the Sounds folder. (There are usually several in the %WINDIR%\Media 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/RandomSound.inc"-->
    <bgsound src="<%=RandomSound("/sounds/","/sounds/default.mid")%>">
    </head>
    <body>
    <h2 align="center">Random Background Sound Test</h2>
    <p>Hit "Refresh" in your browser to change sounds.</p>
    </body>
    </html>
                            

    Note: The call to the RandomSound() function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.

    Create a folder named "Includes" in your Web site's root directory.

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

    <%
      Function RandomSound(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strSound, strPhysical, strFile
    
        ' this constant has the names of valid file extensions
        ' it can be customized for more or less file types
        Const strValid = ".wav.mid.rmi"
    
        ' 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 valid files
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add valid files to a string of file names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the names into an array
        strSound = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strSound) > 1 Then
          ' get a random name
          RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
        Else
          ' otherwise return the default
          RandomSound = strDefault
        End If
    
      End Function
    %>
                            



    When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.

  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/RandomSound.inc"-->
    <bgsound src="<%=RandomSound("/sounds/","/sounds/default.mid")%>">
    </head>
    <body>
    <h2 align="center">Random Background Sound Test</h2>
    <p>Hit "Refresh" in your browser to change sounds.</p>
    </body>
    </html>
                            

    Note: The call to the RandomSound() function passes the name of the default sound as /Sounds/Default.mid. You will need to change that to a valid sound file name and path when you want the function to a return default sound when a folder contains no sound files.

    Create a folder named "Includes" in your Web site's root directory.

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

    <%
      Function RandomSound(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strSound, strPhysical, strFile
    
        ' this constant has the names of valid file extensions
        ' it can be customized for more or less file types
        Const strValid = ".wav.mid.rmi"
    
        ' 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 valid files
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add valid files to a string of file names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the names into an array
        strSound = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strSound) > 1 Then
          ' get a random name
          RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
        Else
          ' otherwise return the default
          RandomSound = strDefault
        End If
    
      End Function
    %>
                            



    When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.

  4. Create a folder named "Includes" in your Web site's root directory.

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

    <%
      Function RandomSound(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strSound, strPhysical, strFile
    
        ' this constant has the names of valid file extensions
        ' it can be customized for more or less file types
        Const strValid = ".wav.mid.rmi"
    
        ' 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 valid files
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add valid files to a string of file names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the names into an array
        strSound = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strSound) > 1 Then
          ' get a random name
          RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
        Else
          ' otherwise return the default
          RandomSound = strDefault
        End If
    
      End Function
    %>
                            



    When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.

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

    <%
      Function RandomSound(strPath,strDefault)
        On Error Resume Next
        Randomize Timer
    
        ' declare all variables
        Dim objFSO, objFolder, objFiles, objFile
        Dim strFiles, strSound, strPhysical, strFile
    
        ' this constant has the names of valid file extensions
        ' it can be customized for more or less file types
        Const strValid = ".wav.mid.rmi"
    
        ' 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 valid files
        For Each objFile in objFiles
          strFile = LCase(objFile.Name)
          If Instr(strValid,Right(strFile,4)) Then
            ' add valid files to a string of file names
            strFiles = strFiles & strFile & vbTab
          End If
        Next
    
        ' split the names into an array
        strSound = Split(strFiles,vbTab)
        
        ' if we have an array...
        If UBound(strSound) > 1 Then
          ' get a random name
          RandomSound = strPath & strSound(Int(Rnd(1)*UBound(strSound)))
        Else
          ' otherwise return the default
          RandomSound = strDefault
        End If
    
      End Function
    %>
                            



    When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.

  6. When you browse the RandomTest.asp page on a computer equipped with a sound card installed you should hear a random background sound file being played. As long as valid files are found in the Sounds folder the background sound should change each time you refresh the page.

More information on Microsoft's scripting technologies can be found at http://msdn2.microsoft.com/en-us/library/ms950396.aspx.

Keywords: kbcodesnippet kbfso kbhowto kbscript KB248027