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

Populating a treeview recursively 1

Status
Not open for further replies.

RPrinceton

Programmer
Jan 8, 2003
86
US
Hi Everyone,
I want to populate a treeview with directories, subdirectories and files. Within each folder I want to have the directories come first followed by any subdirectories. Within each directory or subdirectories I want the files to be presented last as shown below (i.e., Explorer view):
A:\
Folder1
SubFolder1A
SubFolder1B
FileF11
FileF22
Folder2
SubFolder2A
Subfolder2AA
FileSF2AA1
FileSF2AA2
FileSF2A1
FileSF2A2
FileF21
FileF22

I found the snippet of code below in the forum. It works beautifully for building a tree for directories. I attempted to add the code to process the files but it does not build the tree like I have illustrated above. I think it has to do with the "relative" portion of the Add method of the Treeview but I am not sure how it should be done in recursion type processing. I have to believe it is an easy "tweak".
Please advise. Thx in advance.
Regards,
RPrinceton

Code:
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.Name
Set Files = AFolder.Files
For Each File In Files
TreeView1.Nodes.Add node, tvwChild, AFolder & File.Name, File.Name
Next
AddFolder (AFolder)
Next
DoEvents
End Function
 
Change your AddFolders function to this (I've put the changed/added code in bold):

Function AddFolder(node As String)
Dim AFolder As Variant
Dim NewNode As Node

Set TheFolders = localfilesystem.GetFolder(node)
For Each AFolder In TheFolders.SubFolders
Set NewNode = TreeView1.Nodes.Add(node, tvwChild, AFolder, AFolder.Name)
DoEvents
AddFolder (AFolder) 'note: add folders first, then add files
Set Files = AFolder.Files
For Each File In Files
TreeView1.Nodes.Add NewNode, tvwChild, AFolder & File.Name, File.Name
DoEvents
Next
Next
DoEvents
End Function

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Thank you jebenson for your quick response!

FYI: I had to add the "bolded" code to pick up the files in the root path after calling AddFolder.

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 & ":\")
Set TheFolders = LocalFileSystem.GetFolder(ADrive.DriveLetter & ":\")
Set Files = TheFolders.Files
For Each File In Files
TreeView1.Nodes.Add ADrive.DriveLetter & ":\", tvwChild, ADrive.DriveLetter & ":\" & File.Name, File.Name
DoEvents
Next

Next ADrive
End Sub

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

I am new to VB and i have to do a hard core customization in VB and in my code i do have a data type as NODe

Private m_objCurrDocNode. As node
strDocId = Right(m_objCurrDocNode.key, 16)
If m_objCurrDocNode.children > 0

can u tell me the place where i can get all this API
that is the palce where i can get all the inbuild node function or information of Node

ASAP thansk a lot
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top