Navigating folder objects

holdemfoldem
05-15-2003, 02:22 PM
The following code allows me to get a list of sub folders which are in a given folder. It also allows me to go through each and store the name of the sub folder.

But I don't want to go through the entire list of sub folders (currently up to 190 and growing). I just need to know how to access a single folder. I know I can use the Count property to find how many folders there are in the list, but how do I access the nth member of the list?

It's just extremely inefficient to go through each member when I don't need to.

Ok, the code which I have used successfully to go through the entire list of sub folders, (note: "fs" is a global file system object I created when I loaded the main form for this VB app, and "CurrentFolders" is a global variable which contains a valid path):

Dim FGetFolder, FSubFolders, Folder, s
Set FGetFolder = fs.GetFolder(Trim(CurrentFolders) & "\")
Set FSubFolders = FGetFolder.SubFolders
For Each Folder In FSubFolders
s = s & Folder.Name
s = s & vbCrLf
Next
MsgBox s

Thanks ahead for help.

DrPunk
05-16-2003, 05:22 AM
You've stumbled upon a classic case for recursion. You've got the code to check folders and subfolders so not much work needs to be done. I'll explain the methodology rather than giving code, so as not to spoil the fun.

The concept of the idea is that you have a routine that checks the subfolders of a given folder. The first time this is called let's say you pass the route (C:\). Basically, what happens then is that, in the routine that checks for subfolders, every subfolder that it finds it calls itself, therefore checking for any subfolders in the subfolder etc etc.

Consider the following algorithm


Private Sub CheckSubFolders(FolderPath as String)
For each SubFolder In FolderPath
CheckSubFolders(SubFolder)
Next
End Sub

Obviously this doesn't output anything at all (and it isn't really VB. The code won't do anything, it's just trying to prove a point), and you'll need to do some work to get your output in a useful format, but I hope you get the idea.

P.S. It's nice to see someone using the MSR for once. I was begining to think that I was on my own.

holdemfoldem
05-16-2003, 09:33 AM
You've stumbled upon a classic case for recursion. You've got the code to check folders and subfolders so not much work needs to be done. I'll explain the methodology rather than giving code, so as not to spoil the fun.

The concept of the idea is that you have a routine that checks the subfolders of a given folder. The first time this is called let's say you pass the route (C:\). Basically, what happens then is that, in the routine that checks for subfolders, every subfolder that it finds it calls itself, therefore checking for any subfolders in the subfolder etc etc.

Consider the following algorithm


Private Sub CheckSubFolders(FolderPath as String)
For each SubFolder In FolderPath
CheckSubFolders(SubFolder)
Next
End Sub

Obviously this doesn't output anything at all (and it isn't really VB. The code won't do anything, it's just trying to prove a point), and you'll need to do some work to get your output in a useful format, but I hope you get the idea.

P.S. It's nice to see someone using the MSR for once. I was begining to think that I was on my own.

Hmmmm...What I want is to be able to access, say, the nth member of the collection of sub folders (aka: FolderPath). Or perhaps, check through each folder until I find the one I want, without having to iterate through the entire collection.

If the collection were an array, I would do it something like:

dim n as integer
n = 0
do while (n < alen(FolderPath)) and (FolderPath[n].name <> "The folder name I want")
n = n + 1
loop
if n < alen(FolderPath) then
>>>I found my folder, yay! - Continue processing FolderPath[n].name
...
else
>>>Bummer, man
...
endif

The problem is, I don't know how to access individual members of a collection of objects, (in this case, sub folders). And, although it would "work", it just seems so dang inefficient to iterate completely through a potentially long list of objects, just to get at the one I want.

Any ideas? Thanks ahead!

SpaceFrog
05-16-2003, 11:28 AM
Sorry I did not quite follow you all the way thru, but I think this could be usefull
Dim fso As New FileSystemObject
Sub Main()
Dim fd As Folder, PathFolder As String
PathFolder = "C:"
Set fd = fso.GetFolder(PathFolder)
If fso.FolderExists(PathFolder) Then SearchFolder fd
End Sub


Sub SearchFolder(fld As Folder)
Dim f As File, SubFld As Folder
For Each f In fld.Files
Debug.Print f.Name
Next
For Each SubFld In fld.SubFolders
SearchFolder SubFld
Next
End Sub

holdemfoldem
05-16-2003, 11:48 AM
Sorry I did not quite follow you all the way thru, but I think this could be usefull
Dim fso As New FileSystemObject
Sub Main()
Dim fd As Folder, PathFolder As String
PathFolder = "C:"
Set fd = fso.GetFolder(PathFolder)
If fso.FolderExists(PathFolder) Then SearchFolder fd
End Sub


Sub SearchFolder(fld As Folder)
Dim f As File, SubFld As Folder
For Each f In fld.Files
Debug.Print f.Name
Next
For Each SubFld In fld.SubFolders
SearchFolder SubFld
Next
End Sub


Thanks for input, SpaceFrog. The question I'm trying to get answered amounts to not using the For Each construct, since this construct loops through the entire list of subfolders, which is inefficient when all I want is to reference 1 of the subfolders.

In my case, the list is around 200 sub folders and growing daily. Does anyone know of a way to address indidual members of a collection of folders without using For Each to loop through the entire list? (And, FWIW, as a proponent of good structure programming techniques, I would rather not simply break out of the For Each construct upon finding the folder I want to use.)

I figure there's gotta be a way of addressing a given member or members of a collection of subfolders.

Thanks one and all for responses/help.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum