Display the local files in tree view list

How can i list the local system files in a treeview list pls give some idea's thank you
anp_mcaAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
The original code will not work if it is run twice. This new version clears the list before running so that it can be run as many times as you wish.

I have also added some logging. The log file will  be saved in the root of your C: drive, called something like "tvw08042.log". Please attach the file here if the code still does not run properly for you.
Option Explicit
 
Private Sub Command1_Click()
    Dim strTopFolder As String
    Dim Nd As Node
    TreeView1.Nodes.Clear
    strTopFolder = "C:\Winnt"
    WriteLog "*********************************"
    WriteLog "Adding: " & strTopFolder & "  Key: " & strTopFolder
    TreeView1.Nodes.Add , , strTopFolder, strTopFolder
    AppendChildFolders strTopFolder, TreeView1
    TreeView1.Nodes(1).Expanded = True
End Sub
 
Private Sub AppendChildFolders(strPath As String, TreeView1 As TreeView)
    Dim strFolder As String
    Dim strFolders() As String
    Dim iFolderCount As Integer
    Dim i As Integer
    
    strFolder = Dir$(strPath & "\*", vbDirectory)
    Do Until strFolder = ""
        If (GetAttr(strPath & "\" & strFolder) And vbDirectory) = vbDirectory Then
            If Left$(strFolder, 1) <> "." Then
                ReDim Preserve strFolders(iFolderCount)
                strFolders(iFolderCount) = strFolder
                iFolderCount = iFolderCount + 1
            End If
        End If
        strFolder = Dir$()
    Loop
    For i = 0 To iFolderCount - 1
        WriteLog "Adding: " & strFolders(i) & vbTab & "  Key: " & strPath & "\" & strFolders(i)
        TreeView1.Nodes.Add strPath, tvwChild, strPath & "\" & strFolders(i), strFolders(i)
        AppendChildFolders strPath & "\" & strFolders(i), TreeView1
    Next
    DoEvents
End Sub
Sub WriteLog(strText As String)
    Dim f As Integer
    f = FreeFile
    Open "C:\tvw" & Format(Now, "yy") & Format(Format(Now, "y"), "000") & ".log" For Append As #f
        Print #f, Format(Now, "hh:nn ss ") & strText
    Close #f
End Sub

Open in new window

0
 
bsharathCommented:
Try this...

tree /a > c:\tree.txt
0
 
anp_mcaAuthor Commented:
The above single line code  is  enough or not  
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
bsharathCommented:
have you tried it.Is this not what you want
0
 
GrahamSkanRetiredCommented:
This uses VB6 code to fill the tree
Option Explicit
 
Private Sub Command1_Click()
    Dim strTopFolder As String
    Dim Nd As Node
 
    strTopFolder = "C:\Winnt"
    TreeView1.Nodes.Add , , strTopFolder, strTopFolder
    AppendChildFolders strTopFolder, TreeView1
    TreeView1.Nodes(1).Expanded = True
End Sub
 
Private Sub AppendChildFolders(strPath As String, TreeView1 As TreeView)
    Dim strFolder As String
    Dim strFolders() As String
    Dim iFolderCount As Integer
    Dim i As Integer
    
    strFolder = Dir$(strPath & "\*", vbDirectory)
    Do Until strFolder = ""
        If (GetAttr(strPath & "\" & strFolder) And vbDirectory) = vbDirectory Then
            If Left$(strFolder, 1) <> "." Then
                ReDim Preserve strFolders(iFolderCount)
                strFolders(iFolderCount) = strFolder
                iFolderCount = iFolderCount + 1
            End If
        End If
        strFolder = Dir$()
    Loop
    For i = 0 To iFolderCount - 1
        TreeView1.Nodes.Add strPath, tvwChild, strPath & "\" & strFolders(i), strFolders(i)
        AppendChildFolders strPath & "\" & strFolders(i), TreeView1
    Next
    DoEvents
End Sub

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
anp_mcaAuthor Commented:
GrahamSkan: I am used ur code but the strFolder value is null so it could not be active the for loop now wt can i do
0
 
GrahamSkanRetiredCommented:
It works in my tests.
I cannot see how strFolder can be Null. It can be a zero length string, but if it is, it does not get included in the sub-folder search.
Which line gives the error?
0
 
anp_mcaAuthor Commented:
strFolder = Dir$(strPath & "\*", vbDirectory)

In runtime when the code reach this line i got a error message.
0
 
GrahamSkanRetiredCommented:
Does the folder in line 7 exist?

If you don't have C:\WinNT on your system, have you changed it to the one that you want loaded into the TreeView?

If it does exist, what is the error message number, please? And the message text, if it is in English?
0
 
anp_mcaAuthor Commented:
I would try to change the directory name but it seems the error.If the code need to set the properties, pls tell me .Then the error message is "Runtime error 35602". When i click the command button the tree view show the given dir name ex: c:\ .
0
 
anp_mcaAuthor Commented:
More than one time i clicked the button that time i got the error message " the key is not a unique collecction."
0
 
anp_mcaAuthor Commented:
I got one error imagelist must be initialized before it is used. tell me the solution for remove the error . already i set the properties in listview control.
0
 
anp_mcaAuthor Commented:
Ok
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
All Courses

From novice to tech pro — start learning today.