ASPFAQs.com
Your source for ASP-related frequently asked questions and answers!


4GuysFromRolla.com : ASP FAQS : FileSystemObject

Question: How can I programmatically list the contents of a directory on the Web server?

Answer: With the FileSystemObject you can (fairly easily) programmatically obtain all of the files of a given directory on the Web server. To accomplish this task we'll need to complete the following steps:

1.) Create an instance of the FileSystemObject object
2.) Use the FileSystemObject to return a Folder object (which contains information about a particular directory).
3.) Use a For Each ... Next loop to iterate through each file in the Folder object.

So, let's examine some code that will handle this...

<%
  'Create the FileSystemObject object
  Dim objFSO
  Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

  'Obtain an folder object instance for a particular directory
  Dim objFolder
  Set objFolder = objFSO.GetFolder("C:\FooBar\Blah\")

  'Use a For Each ... Next loop to display the files
  Dim objFile
  For Each objFile in objFolder.Files
     'Print out the name
     Response.Write objFile.Path & "<BR>"
  Next
%>

That's it! The Folder object contains a slew of information about a particular folder (in this case, we obtained information about the C:\FooBar\Blah\ directory. If you want to get a listing of subfolders of a folder, you can use the same technique to iterate through the SubFolders collection of the Folder object.

There is a great article on 4Guys that explores a very practical application that relies on the ability to recursively iterate through the contents of a particular directory. It's definitely worth a read if you need to learn more about this technique!
Generating a Web Site File System Report.

For more information, be sure to also check out these resources:
-- FileSystemObject Object Technical Docs.
-- Folder Object Technical Docs.
-- FileSystemObject FAQs on ASPFAQs.com

Happy Programming!


FAQ posted by Scott Mitchell at 11/24/2000 3:57:36 PM to the FileSystemObject category. This FAQ has been viewed 69,860 times.


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers