HOWTO: Traverse Through a Collection Using JScript or JavaScript

ID: Q229693


The information in this article applies to:


SUMMARY

This article describes how to traverse through a collection using VBScript and JScript or JavaScript. This article also uses Scripting.FileSystemObject to demonstrate traversing through its file collection using script.

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.

These samples demonstrate traversing through a collection using VBScript and JScript or JavaScript using server-side scripts on Active Server Pages (ASP).


MORE INFORMATION

These samples traverse a directory (in this case, C:\Text) and list all the files located in the directory. For this demonstration, a directory called Text has been created in the C drive's root directory, and five text files have been placed within the Text directory.

Let's begin by traversing through the Scripting.FileSystemObject using VBScript. The VBScript code below uses a FOR EACH...NEXT loop to traverse through the FileCollection. Using a Web page editor such as Visual InterDev, create a new ASP page. Delete all the tags and scripts on the newly created ASP page, and paste the following VBScript:


<% @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

%> 
Now, let's do the equivalent using JScript or JavaScript. In order to do this, you must use an enumerator object as shown below. Using a Web page editor such as Visual InterDev, create a new ASP page. Delete all the tags and scripts on the newly created ASP, and paste the following JScript or JavaScript.

<% @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;

Also, the output for each script appears differently. In VBScript, the output shows only the file name and its file extension as shown below:

VBScript Method
Number of files found: 5
test1.txt
test2.txt
test3.txt
test4.txt
test5.txt 
However, in JScript or JavaScript, the output below displays the physical directory, 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 


REFERENCES

For more information on JScript and VBScript, please refer to the Microsoft Scripting Web site.

Additional query words: kbASP kbGrpASP kbJScript


Keywords          : kbASP kbJScript kbScript kbVBScript kbGrpASP 
Version           : winnt:
Platform          : winnt 
Issue type        : kbhowto 

Last Reviewed: May 24, 1999