- 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 %>
<% @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; %>
FOR(initialize ; test ; increment)
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
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.
Article ID: 229693 – Last Review: July 19, 2012 – Revision: 3.0
|kbfso kbhowto kbscript KB229693|
Continue reading here: