Treeview Files/Folder Lists Recursively

You will need a command button called Command1 to start the scan and a treeview control called Treeview1.
This example scans the path of a directory listbox (Dir1)

It will also support pattern matching. ie: only scanning certain types of files, like *.jpg or *.exe.

Recursion is when a routine calls itself..  If a subfolder is found, then RecurseFiles2 calls itself to scan the sub folder/s encountered.

' In general Declarations
   Dim tvn As Node

Private Sub Command1_Click()
   Me.MousePointer = vbHourglass

   ' Pathname to create folder/file list from
   p$ = Dir1.Path
   If Right$(p$, 1) <> "\" Then p$ = p$ + "\"
   Set tvn = TreeView1.Nodes.Add(, tvwParent, p$, p$)
   RecurseFiles2 p$
   Me.MousePointer = vbDefault
End Sub

Sub RecurseFiles2(ByVal fPath As String)
   Dim File_Name As String
   Dim File_Read As Integer         ' Number of Files Read
   Dim strTempPath As String
   Dim i As Integer

   If Right$(fPath, 1) <> "\" Then fPath = fPath & "\"
   ' to do a pattern match do, or something.
   ' folders won`t be included in the list (not my fault)
   ' File_Name = Dir$(fPath+"\*.exe", vbDirectory)
   File_Name = Dir$(fPath, vbDirectory)
   File_Read = 1

   Do While File_Name <> ""
       If File_Name <> "." And File_Name <> ".." Then
           strTempPath = fPath & File_Name
           If GetAttr(strTempPath) And vbDirectory Then
               Set tvn = TreeView1.Nodes.Add(fPath, tvwChild, strTempPath + "\", File_Name)
               RecurseFiles2 strTempPath    ' if a folder, then call this routine to scan that folder (recursion)
               File_Name = Dir$(fPath, vbDirectory)
               For i = 2 To File_Read
                   File_Name = Dir$
               Set tvn = TreeView1.Nodes.Add(fPath, tvwChild, strTempPath, File_Name)
           End If
       End If
       File_Name = Dir$
       File_Read = File_Read + 1
End Sub


You might also like...


Kym Manson Till the Roof comes off Till the Lights go out Till my Legs give out Can't shut my mouth I will not fall, my Wisdoms all.


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann