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
 
 
 
 
 
TitleSearch a directory hierarhcy and list the files' names, creation dates, and sizes by using the File System Object
DescriptionThis example shows how to search a directory hierarhcy and list the files' names, creation dates, and sizes by using the File System Object in Visual Basic 6.
Keywordsdirectory hierarhcy, file names, File System Object, FSO, creation date, FileSystemObject
CategoriesFiles and Directories, Controls
 
When the user clicks the Search button, the program clears its TreeView control. It creates a FileSystemObject and gets the target folder. It creates a TreeView node for that folder and then calls subroutine ListFileInfo.

Subroutine ListFileInfo loops over a folder's subfolders, recursively calls itself to describe them. It then loops over the folder's files, adding them to the TreeView.

 
Private Sub cmdSearch_Click()
Dim fso As FileSystemObject
Dim target_folder As Folder
Dim target_node As Node
Dim txt As String

    Screen.MousePointer = vbHourglass
    trvResults.Visible = False
    DoEvents

    ' Clear the TreeView.
    trvResults.Nodes.Clear

    ' Get the starting folder.
    Set fso = New FileSystemObject
    Set target_folder = fso.GetFolder(txtDir.Text)

    ' Add the starting folder to the TreeView.
    txt = _
        target_folder.ParentFolder & "\" & _
            target_folder.Name & _
        " (" & target_folder.DateCreated & ", " & _
        FormatBytes(target_folder.Size) & ")"
    Set target_node = trvResults.Nodes.Add(, , , txt)

    ' Search.
    ListFileInfo trvResults, target_node, target_folder

    trvResults.Visible = True
    Screen.MousePointer = vbDefault
End Sub

' List the information about this directory under
' the TreeView node.
Private Sub ListFileInfo(ByVal trv As TreeView, ByVal _
    parent_node As Node, ByVal parent_folder As Folder)
Dim txt As String
Dim child_folder As Folder
Dim child_file As File
Dim child_node As Node

    ' Search subdirectories.
    For Each child_folder In parent_folder.SubFolders
        txt = _
            child_folder.Name & _
            " (" & child_folder.DateCreated & ", " & _
            FormatBytes(child_folder.Size) & ")"
        Set child_node = trv.Nodes.Add( _
            parent_node, tvwChild, , txt)
        ListFileInfo trv, child_node, child_folder
    Next child_folder

    ' List the files.
    For Each child_file In parent_folder.Files
        txt = _
            child_file.Name & _
            " (" & child_file.DateCreated & ", " & _
            FormatBytes(child_file.Size) & ")"
        trv.Nodes.Add parent_node, tvwChild, , txt
    Next child_file
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated