How To Use JScript or JavaScript to Traverse Through a Collection
- 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
")
'Display the number of files within the Text directory
Response.Write("Number of files found: " & FileCollection.Count & "
")
'Traverse through the FileCollection using the FOR EACH...NEXT loop
For Each FileName in FileCollection
strFileName = FileName.Name
Response.Write(strFileName & "
")
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
");
// Display the number of files within the Text directory
Response.Write("Number of files found: " + FileCollection.Count + "
");
// Traverse through the FileCollection using the FOR loop
for(var objEnum = new Enumerator(FileCollection); !objEnum.atEnd(); objEnum.moveNext())
strFileName = objEnum.item();
Response.Write(strFileName + "
");
// 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:Texttest1.txt C:Texttest2.txt C:Texttest3.txt C:Texttest4.txt C:Texttest5.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.
(http://technet.microsoft.com/library/ee176792.aspx)
Web site.
Article ID: 229693 – Last Review: July 19, 2012 – Revision: 3.0
| kbfso kbhowto kbscript KB229693 |
Continue reading here:
How To Use JScript or JavaScript to Traverse Through a Collection
Recent Comments