Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDisplay a directory structure in a TreeView controls, and save and restore the hierarchy in a file
DescriptionThis example shows how to display a directory structure in a ListView control in Visual Basic 6. It also saves the structure in a text file using indentation to show the hierarchy's structure and shows how to load the structure back into a TreeView control.
KeywordsTreeView, directory, directory hierarchy
CategoriesControls, Files and Directories
 
Thanks to Sergio Perciballi.

When you click the Go button, the program gets ready, opens a text file, and calls subroutine Get_Files.

 
Private Sub cmdGo_Click()
    Dim x As Integer, S$
    Dim file_name As String

    file_name = App.Path
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"
    Open file_name & "Mytest.txt" For Output As #1

    Text1.Text = ""
    TreeView1.Nodes.Clear

    file_name = txtPath.Text
    If Right$(file_name, 1) <> "\" Then file_name = _
        file_name & "\"

    Text1.Text = Text1.Text & file_name & vbCrLf
    ' need \ on end of key here------------|
    Set fnode = TreeView1.Nodes.Add(, , file_name, _
        file_name)
    'initial data to file; topmost root of tree
    Print #1, file_name
    'initialise variables
    FIndent = 1
    FIndex = 0
    Text1.Visible = False
    TreeView1.Visible = False
    LblPrompt.Caption = "Processing drive..."
    LblPrompt.Refresh
    'note does not end with '\'
    StrtPath = Left$(file_name, Len(file_name) - 1)
    'start the directory reading
    Get_Files StrtPath
    Text1.Visible = True
    TreeView1.Visible = True
    LblPrompt.Caption = "Finished"

    Close 1

End Sub
 
Subroutine Get_Files uses the Dir$ command to get the subdirectories inside a directory. It prints the subdirectory names into the text file and adds nodes for them in the TreeView control. It recursively calls itself to get the files contained in the subdirectory.
 
Private Sub Get_Files(FPath As String)
    Dim file_name As String
    Dim File_Path As String
    Dim File_Read As Integer
    Dim x As Boolean, xTemp As Integer, S$
    Dim I As Integer
    On Error Resume Next
    FIndent = FIndent + 1
    File_Path = FPath & "\"
    file_name = Dir$(File_Path, vbDirectory)
    File_Read = 1
    x = False

    Do While file_name <> ""
        If file_name <> "." And file_name <> ".." Then
            If GetAttr(File_Path & file_name) <> _
                vbDirectory Then
                FIndex = FIndex + 1
            Else
                StrtPath = File_Path & file_name

                Set fnode = TreeView1.Nodes.Add(File_Path, _
                    tvwChild, FPath & "\", file_name)

                'changed to dash/hyphen for readability
                Text1.Text = Text1.Text & "->" & _
                    String(FIndent * FIndent, "_") & _
                    file_name & vbCrLf
                S$ = ""
                ''possible hack ; if FIndent=1 this doesn't
                ' execute
                For xTemp = 2 To FIndent
                    S$ = S$ & vbTab
                Next
                'write to a file; number of levels= number
                ' of tabs
                Print #1, S$ & file_name

                FIndex = FIndex + 1
                x = True
                'recursive call
                Get_Files StrtPath

            End If

        End If
        If x = True Then
            file_name = Dir$(File_Path, vbDirectory)
            For I = 2 To File_Read
                file_name = Dir$
            Next
            x = False
        End If
        file_name = Dir$
        File_Read = File_Read + 1

    Loop
    FIndent = FIndent - 1

End Sub
 
Subroutine LoadTreeViewFromFile loads the text file into a TreeView control. It uses "Line Input" to read the files' lines. For each line, it counts the number of Tab characters at the beginning of the line and adds the new directory name to the appropriate level in the TreeView control.
 
Private Sub LoadTreeViewFromFile(ByVal file_name As String, _
    ByVal trv As TreeView)
    Dim fnum As Integer
    Dim text_line As String
    Dim level As Integer
    Dim tree_nodes() As node
    Dim num_nodes As Integer
    
    fnum = FreeFile
    Open file_name For Input As fnum

    trv.Nodes.Clear
    Do While Not EOF(fnum)
        ' Get a line.
        Line Input #fnum, text_line

        ' Find the level of indentation.
        level = 1
        Do While Left$(text_line, 1) = vbTab
            level = level + 1
            text_line = Mid$(text_line, 2)
        Loop

        ' Make room for the new node.
        If level > num_nodes Then
            num_nodes = level
            ReDim Preserve tree_nodes(1 To num_nodes)
        End If

        ' Add the new node.
        If level = 1 Then
            Set tree_nodes(level) = trv.Nodes.Add(, , , _
                text_line)
        Else
            Set tree_nodes(level) = _
                trv.Nodes.Add(tree_nodes(level - 1), _
                tvwChild, , text_line)
            'tons faster without this
            ''tree_nodes(level).EnsureVisible
        End If
    Loop

    Close fnum

End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated