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.