Treeview Search Routine
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
Private Sub UserForm_Click()
End Sub
Private Sub UserForm_Initialize()
txtDir.Text = ThisWorkbook.Path
End Sub
' Return a formatted string representing the number of
' bytes.
Private Function FormatBytes(ByVal num_bytes As Double) As String
Const ONE_KB As Double = 1024
Const ONE_MB As Double = ONE_KB * 1024
Const ONE_GB As Double = ONE_MB * 1024
Const ONE_TB As Double = ONE_GB * 1024
Const ONE_PB As Double = ONE_TB * 1024
Const ONE_EB As Double = ONE_PB * 1024
Const ONE_ZB As Double = ONE_EB * 1024
Const ONE_YB As Double = ONE_ZB * 1024
Dim value As Double
Dim txt As String
' See how big the value is.
If num_bytes <= 999 Then
' Format in bytes.
FormatBytes = Format$(num_bytes, "0") & " bytes"
ElseIf num_bytes <= ONE_KB * 999 Then
' Format in KB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_KB) & " KB"
ElseIf num_bytes <= ONE_MB * 999 Then
' Format in MB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_MB) & " MB"
ElseIf num_bytes <= ONE_GB * 999 Then
' Format in GB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_GB) & " GB"
ElseIf num_bytes <= ONE_TB * 999 Then
' Format in TB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_TB) & " TB"
ElseIf num_bytes <= ONE_PB * 999 Then
' Format in PB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_PB) & " PB"
ElseIf num_bytes <= ONE_EB * 999 Then
' Format in EB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_EB) & " EB"
ElseIf num_bytes <= ONE_ZB * 999 Then
' Format in ZB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_ZB) & " ZB"
Else
' Format in YB.
FormatBytes = ThreeNonZeroDigits(num_bytes / _
ONE_YB) & " YB"
End If
End Function
' Return the value formatted to include at most three
' non-zero digits and at most two digits after the decimal
' point. Examples:
' 1
' 123
' 12.3
' 1.23
' 0.12
Private Function ThreeNonZeroDigits(ByVal value As Double) _
As String
If value >= 100 Then
' No digits after the decimal.
ThreeNonZeroDigits = Format$(CInt(value))
ElseIf value >= 10 Then
' One digit after the decimal.
ThreeNonZeroDigits = Format$(value, "0.0")
Else
' Two digits after the decimal.
ThreeNonZeroDigits = Format$(value, "0.00")
End If
End Function
' 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
'Ensure visible
child_node.EnsureVisible
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
Share This