How To Use JScript or JavaScript to Traverse Through a Collection (229693)
The information in this article applies to:
- Microsoft Active Server Pages
- Microsoft Internet Information Server
This article was previously published under Q229693 SUMMARY
This article describes how to use server-side scripts on Active Server Pages (ASP) pages with Visual Basic Script (VBScript) and JScript or JavaScript to traverse through a collection. In VBScript, you can use the FOR EACH...NEXT loop to traverse through a collection. In JScript or JavaScript, you must use an enumerator object.
MORE INFORMATION
The examples in this article use the file system object to traverse a folder (in this case, C:\Text) and list all the files located in the folder. The first example uses a FOR EACH...NEXT loop in VBScript to traverse:
- Create a new folder in the root folder of drive C and name it "Text".
- Place five text files in the directory you just created.
- Create a new ASP page and add the following VBScript code:
<% @LANGUAGE="VBScript" %>
<%
'Reference the FileSystemObject
set FSO = Server.CreateObject("Scripting.FileSystemObject")
'Reference the Text directory
set Folder = FSO.GetFolder("C:\Text")
'Reference the File collection of the Text directory
set FileCollection = Folder.Files
Response.Write("VBScript Method<BR>")
'Display the number of files within the Text directory
Response.Write("Number of files found: " & FileCollection.Count & "<BR>")
'Traverse through the FileCollection using the FOR EACH...NEXT loop
For Each FileName in FileCollection
strFileName = FileName.Name
Response.Write(strFileName & "<BR>")
Next
'De-reference all the objects
set FileCollection = Nothing
set Folder = Nothing
set FSO = Nothing
%>
The following example demonstrates the equivalent but uses JScript or JavaScript and the enumerator object as shown below. Follow the steps outlined previously, except use the following code in step 3.
<% @LANGUAGE="JScript" %>
<%
// Reference the FileSystemObject
var FSO = Server.CreateObject("Scripting.FileSystemObject");
// Reference the Text directory
var Folder = FSO.GetFolder("c:\\Text");
// Reference the File collection of the Text directory
var FileCollection = Folder.Files;
Response.Write("JScript Method<BR>");
// Display the number of files within the Text directory
Response.Write("Number of files found: " + FileCollection.Count + "<BR>");
// Traverse through the FileCollection using the FOR loop
for(var objEnum = new Enumerator(FileCollection); !objEnum.atEnd(); objEnum.moveNext()) {
strFileName = objEnum.item();
Response.Write(strFileName + "<BR>");
}
// Destroy and de-reference enumerator object
delete objEnum;
objEnum = null;
// De-reference FileCollection and Folder object
FileCollection = null;
Folder = null;
// Destroy and de-reference FileSystemObject
delete FSO;
FSO = null;
%>
NOTE: The enumerator object is instantiated within the FOR loop, which is okay in JScript or JavaScript. The syntax for the FOR statement is as follows:
FOR(initialize ; test ; increment) statement;
The output for each example in this article will appear differently. In VBScript, the output shows only the file name and its file extension as shown here:
VBScript Method
Number of files found: 5
test1.txt
test2.txt
test3.txt
test4.txt
test5.txt
In JScript or JavaScript, the output displays the physical folder, the file name, and its file extension:
JScript Method
Number of files found: 5
C:\Text\test1.txt
C:\Text\test2.txt
C:\Text\test3.txt
C:\Text\test4.txt
C:\Text\test5.txt
The third-party products that are discussed in this article are manufactured by companies that are independent of Microsoft. Microsoft makes no warranty, implied or otherwise, regarding the performance or reliability of these products. REFERENCES
For more information on JScript and VBScript, please refer to the Microsoft Scripting Web site.
Modification Type: | Minor | Last Reviewed: | 7/13/2004 |
---|
Keywords: | kbFSO kbhowto kbScript KB229693 |
---|
|