RPrinceton
Programmer
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
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