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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through SubFolders

Status
Not open for further replies.

Ddraig

MIS
Jul 16, 2004
97
US
Howdy,

I am trying to figure out a way to loop through subfolders, with in a folder. I've got the code that will loop through an inital folder but I was wondering if anyone had suggestions on how to go about subfolders efficently and what the best way or ways that have worked for them?


This is the code I have so far. I'm thinking I need to just have it loop back with a new strPath?

Dim strPath As String
strPath = Server.MapPath("images")
Dim FolderCollection As String()

Dim i As Integer
FolderCollection = Directory.GetDirectories(strPath)

Dim bloExists As Boolean

For i = 0 To FolderCollection.Length - 1
myDirInfo = New DirectoryInfo(FolderCollection(i))
Response.Write("<a href='images\" & myDirInfo.Name & "'>" & myDirInfo.Name & "</a><br>")



Next
 
Yes, you are pretty much there. If you create a function that loops through all files in a folder, you simply have to call it recursively if any subfolders exist.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Here is an ASP page I've been using for years. It writes each file to the page as a hyperlink. You should be able to see how the recrusion is handled.

Code:
<HTML>
<HEAD>
<TITLE>List Files</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF">

<%

Set FSO = Server.CreateObject("Scripting.FileSystemObject")

ShowSubfolders FSO.GetFolder(Server.MapPath("/"))

Sub ShowSubFolders(Folder)
 Response.write "<h3>" & Folder & "</h3>"
 ShowFiles Folder
 For Each Subfolder in Folder.SubFolders
  Response.write "<h3>" & Subfolder.Path & "</h3>"
  ShowFiles Subfolder.Path
  ShowSubFolders Subfolder
 Next
End Sub

Sub ShowFiles(Subfolder)
 Set MyFolder = FSO.GetFolder(Subfolder)
 For each f1 in MyFolder.Files
  response.write("<a href='" & SubFolder & "/" & f1.name & "'>" & f1.name & "</a><BR>")
 Next
End Sub

%>
</BODY>
</HTML>

 
Awsome I think I got it working here. :)


Public Sub getSubFolders(ByVal sender As String)
Dim subFolderCollection As String()
fPath = strPath & "\" & sender
subFolderCollection = Directory.GetDirectories(fPath)
Dim i As Integer
For i = 0 To subFolderCollection.Length - 1
myDirInfo = New DirectoryInfo(subFolderCollection(i))
Response.Write("<a href='images/" & sender & "/" & myDirInfo.Name & "'><img src=images/sub.jpg border=0>" & myDirInfo.Name & "</a><br>")
fPath = sender & "\" & myDirInfo.Name

Dim label2 As New Label
Me.Controls.Add(label2)
label2.Text = "<br>--: " & strPath & " " & fPath & " " & myDirInfo.Name & "<br>"

getSubFolders(fPath)
Next



End Sub

Thats what I have and it loops through perfectly. Although I am curious to see if you've got any ideas on how I might accomplish my next bit. That is for every sub folder to indent.

As it stands now the code goes through the folder list, finds a folder then looks for subfolders, and then so on until it doesn't find any subs, then moves to the next folder. I can't for the life of me figure out how to indent them correctly.
 
What files and folders are you actually looping through and for waht purpose (as there may be an easier way to acheive what you are wanting to do)?


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
That is what I'm wondering.

As it stands right now my original intent was simply to be able to get the folder paths, so that my little image program could display the images for a particular folder.

So now that I accomplished that I was hoping to be able to make a treeview type of thing that would display the folder lists, and indent them correctly.

Here is an example of what it comes out looking like, and what it should look like.

 
You still don't say what files you are listing and for what purpose but I'll take a guess and say you may want to check out this article:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
I did mention it but I guess I will elaborate on it. :) The files are image files its for a picture gallery. When someone clicks on the folder (will convert those to proper buttons) it will send the path to the page to display the image gallery appropriate for that folder that was clicked. Sort of like creating albums.

I looked at this and didn't think it really applied to what I needed it for. But will take a look again.
 
I'll post my code in a little bit but my brian is fried hehe. Haven't had my morning Mt. Dew muahah. Thanks again for the help has to be the best forum around.
 
Try this:

Code:
<HTML>
<HEAD>
<TITLE>List Files</TITLE>
</HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#FF0000" VLINK="#800000" ALINK="#FF00FF">

<%

dim myTab

Set FSO = Server.CreateObject("Scripting.FileSystemObject")

ShowSubfolders FSO.GetFolder(Server.MapPath("/cagesbydesi/wizard_divider")), mytab

Sub ShowSubFolders(Folder, myTab)
myTab = myTab & "&nbsp;&nbsp;"
 Response.write myTab & "<b>" & Folder & "</b><BR>"
 ShowFiles Folder, myTab
 For Each Subfolder in Folder.SubFolders
  Response.write "<b>" & Subfolder.Path & "</b><BR>"
  ShowFiles Subfolder.Path, myTab
  ShowSubFolders Subfolder, myTab
 Next
End Sub

Sub ShowFiles(Subfolder, myTab)
 Set MyFolder = FSO.GetFolder(Subfolder)
 For each f1 in MyFolder.Files
  response.write(myTab & "<a href='" & SubFolder & "/" & f1.name & "'>" & f1.name & "</a><BR>")
 Next
End Sub

%>
</BODY>
</HTML>

 
But odn't use the Response.Write sections from the above code (or the <b>, <BR> and "&nbsp" tags). Instead, create a new label (or Literal) control, add the details to it and assign it a class for any formatting.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Brilliant! That code was what I needed. Apparently yesterday I was on the right track but I didn't think to send the (myTab) variable back. Duh! Wonders of a good night sleep.
 
Also, you should realise that the above code is classic ASP code, not ASP.NET.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
OK, as long as you know. However, I would have thought that as you asked the question in an ASP.NET forum, you would have wanted to implement an ASP.NET solution...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yea basically the code just gave me the extra little bit to get it to get my current code which I posted earlier to work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top