Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileSystemObject object and the Folders collection of the Drive object 1

Status
Not open for further replies.

fchan

MIS
Jul 2, 2001
47
US
Does anyone know how I can use the FileSystemObject object to list the folders in a drive. For example, I want to list all the folders on the c drive of my web server.

I tried using the following code, but got an error message stating that the object does not support this property or method, "Folders".

Code*************************

dim myfso, mydrive, myfolders
set myfso = server.createobject("Scripting.FileSystemObject")
if myfso.driveexits("C") then
set mydrive = myfso.getdrive("C")
for each myfolders in mydrive.folders
response.write myfolders.name & &quot;<br>&quot;
next
else
response.write &quot;false&quot;
end if

end code************************

Am I using the folders collection right?
 
I think you need to use the Folders object and not the Drive object. The Folders object has a SubFolders property that will give you a collection of the SubFolders in a given folder. However, it will only give you the first level of subfolders, to get subfolders within those, you need to define another folder object. In the first example, it lists only the folders off the root:

dim myfso, mydrive, myfolders, folderset myfso = server.createobject(&quot;Scripting.FileSystemObject&quot;)
if myfso.driveexists(&quot;c&quot;) then
set myfolders = myfso.getfolder(&quot;c:&quot;)
for each folder in myfolders.SubFolders
response.write &quot;<br><br>&quot; & folder.name & &quot;<br>&quot;
next
else
response.write &quot;false&quot;
end if

Next example also gives one more level of subfolders. I didn't take it any further than this, but I'm sure if you needed you could check that folders exist and keep going to however many levels of subfolders exist.

dim myfso, mydrive, myfolders, folder, mysubfolders, subfolder
set myfso = server.createobject(&quot;Scripting.FileSystemObject&quot;)
if myfso.driveexists(&quot;c&quot;) then
set myfolders = myfso.getfolder(&quot;c:&quot;)
for each folder in myfolders.SubFolders
response.write &quot;<br><br>&quot; & folder.name & &quot;<br>&quot;
set mysubfolders = myfso.getfolder(folder.name)
for each subfolder in mysubfolders.SubFolders
response.write &quot;{sub} &quot; & subfolder.name & &quot;<br>&quot;
next
next
else
response.write &quot;false&quot;
end if

*** Hope this helps!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top