|
 |
 |
 |
 |
Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms
of Use |
Trademarks
Article ID: 248027 - Last Review: July 3, 2008 - Revision: 6.1 How to Change Background Sounds Randomly Using ASPThis article was previously published under Q248027 We strongly recommend that all users upgrade to Microsoft Internet Information Services (IIS) version 7.0 running on Microsoft Windows Server 2008. IIS 7.0 significantly increases Web infrastructure security. For more information about IIS security-related topics, visit the following Microsoft Web site: For more information about IIS 7.0, visit the following Microsoft Web site: 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. 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.
- 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="http://support.microsoft.com/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. - 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="http://support.microsoft.com/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. - Save the following ASP page as "RandomTest.asp" in your Web
site's root folder:
<% @Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<!--#include virtual="http://support.microsoft.com/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. - 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. - 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. - 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
(http://msdn2.microsoft.com/en-us/library/ms950396.aspx)
.
APPLIES TO- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Services 5.0
| kbcodesnippet kbfso kbhowto kbscript KB248027 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
|
 |
 |
 |
 |
 |
 |
 |
| |