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

FileSystemObject

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
I'm trying to use the FileSystemObject to give me an entire system directory structure. I use:

For Each ADrive In LocalFileSystem.Drives
[ADD NODE TO DIRECTORY TREE]
If ADrive.IsReady Then
Set TheFolders = LocalFileSystem.GetFolder(ADrive.DriveLetter & ":\")
For Each AFolder In TheFolders.SubFolders
[ADD CHILD NODE TO DIRECTORY TREE]
Next
End If
Next

where [ADD NODE TO DIRECTORY TREE] and [ADD CHILD NODE TO DIRECTORY TREE] represent code for a 3rd party tree control. This works fine giving me a list of drives and the first level of folders in each drive. HOWEVER how would I go through every subfolder of each folder and then every level of folders after that?????

Any help would be much appreciated!

elziko
 
What about something like:

Function AddFolder(node)
For Each ADrive In LocalFileSystem.Drives
[ADD NODE TO DIRECTORY TREE]
If ADrive.IsReady Then
Set TheFolders = LocalFileSystem.GetFolder(ADrive.DriveLetter & ":\")
For Each AFolder In TheFolders.SubFolders
AddFolder (AFolder)
Next
End If
Next
End Function


i.e. call the function recursively...

 
Hi Again

That was not really thought through -sorry. This should work:

------------------------------------------------------------
'Place a treeview and a command button on a form.
Dim LocalFileSystem
Dim ADrive As Variant

Private Sub Command1_Click()
Set LocalFileSystem = CreateObject("Scripting.FileSystemObject")
For Each ADrive In LocalFileSystem.Drives
If ADrive.IsReady Then
TreeView1.Nodes.Add , , ADrive.DriveLetter & ":\", ADrive
AddFolder (ADrive.DriveLetter & ":\")
End If
Next ADrive
End Sub

Function AddFolder(node As String)
Dim AFolder As Variant
Set TheFolders = LocalFileSystem.GetFolder(node)
For Each AFolder In TheFolders.SubFolders
TreeView1.Nodes.Add node, tvwChild, AFolder, AFolder
AddFolder (AFolder)
Next
DoEvents
End Function
------------------------------------------------------------

good luck
;-) Sunaj
 
What Sunaj is suggesting is called recursion and yes, that is the way you want to do your scan. - Jeff Marler B-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top