Library code snippets
Treeview Files/Folder Lists Recursively
By Kym Manson, published on 06 Mar 2002
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.
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
TreeView1.Nodes.Clear
' 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$
Next
Else
Set tvn = TreeView1.Nodes.Add(fPath, tvwChild, strTempPath, File_Name)
End If
End If
File_Name = Dir$
File_Read = File_Read + 1
Loop
End Sub
/html>
Related articles
Related discussion
-
Problem with migration to C# (CoCreateInstanceEx)
by LRollison (1 replies)
-
VB6 Problem Creating Shortcuts
by rb1177 (0 replies)
-
how can i open a file
by kyawswarhtun (0 replies)
-
how to save any one form what i want?
by blackguy (5 replies)
-
Build an MP3 Player
by soybees (4 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
Cool,
As Sengoku knows, i used her code in my prog and it works great!
A perfect example of how to fill a treeview control with a directory structure.
Just one thing, i don't think you included pattern matching in your example.
Stevesoft
This thread is for discussions of Treeview Files/Folder Lists Recursively.