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!

FSO recursive directory listing

Status
Not open for further replies.

scotth4v

Programmer
Apr 17, 2001
103
US
Hi Gang, I have written a little page for our intranet that just spits all of the contents of a folder onto a webpage w/links so that I don't have to maintain it when marketing adds docs, etc. Well, what I'd like to do is make it recursive, so if they add subfolders, it will also list them and their contents. I can see how to hardcode it, but how could I loop it so that they can add N number of subdirectories? I had thought about listing just the subfolder names and then having them click the name and have it load the same ASP page showing a difft directory, files, and subfolders, but for now, I'd like to have it on the same page. Here's the current code I'm using to get a folder, its files and subdirs.
Code:
<% 

Dim fso, folder, folderspec, path, link, docs, subs

folderspec=&quot;c:/&quot;

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set folder = fso.GetFolder(folderspec)
Set subs = folder.subfolders

For Each FileName In folder.files
   link = &quot;xxx&quot;
   Response.write &quot;<a href=&quot;&quot;&quot; & link & &quot;&quot;&quot;>&quot; & fileName.name & &quot;</a><br>&quot;
Next
response.write &quot;<b>Subfolders:<br></b>&quot;
For Each FolderName In subs
     response.write folderName.name & &quot;<br>&quot;   
Next

set subs = nothing
set folder = nothing
set fso = nothing

 %>

Thanks a bunch!

-Scott
 
Oh, and pseudo-code is fine, I'm mostly looking for looping structure ideas, not necessarily the code... Thanks!

-Scott
 
I did what you're looking for by making the folder links recall the same asp page while passing it a variable called &quot;NewPath&quot;.

Then in my code, I checked to see if the NewPath that was passed was the root path or a lower level path and then created a link to take the user back to the root folder, to the parent folder, to any folders in the current selected folder, then listed the files with links that would open the individual files.

The biggest trick was determining how to string manipulate the NewPath variable to get the parent directory. For example, if you're opening a folder that is a subfolder of the root directory, the parent directory and the root directory are the same thing. You probably don't want to show two links to the same place (1 for the root & 1 for the parent folder).

Hope that helps.
 
Yeah, thanks, that's what I ended up doing (passing a parameter to the same page)

My link to the parent dir consists of a javascript history.back() call on a button :)

I guess initially, I wanted to be able to do it all w/o reloading the page, but it works fine as is.

Thanks a bunch!

-Scott
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top