I’m working on this app to gather some file/folder usage data. The goal is to have my object know the size of its files and children folders; at every point in the tree.
My problem is; after getting the directory structure into my objects, I’m perplexed at how to get the subtotals in there.
Could I actually add the totals in when creating the trees? Or do I have to step through the tree in reverse (which seems especially confusing…)
I’m a novice with no computer science education; I’m sure the answer would be out there if I knew the right terminology to Google.
My classes are currnetly a bit of a mess, they’ve still got some half formed ideas there…
Imports System.IO
Public Class Form1
Public Class mrFURDirectory
Public Property Name As String
Public Property FileSize As Long = 0
Public Property FileCount As Long = 0
Public Property DirSize As Long = 0
Public Property DirCount As Long = 0
Public Property Parent As mrFURDirectory
Public Property Children As New List(Of mrFURDirectory)
Public Property PercentOfParent As Short = 0
Public Property DirChecked As Boolean = False
Public Sub New(ByVal Name As String)
Me.Name = Name
End Sub
End Class
Public Class mrBaseFURDirectory
Inherits mrFURDirectory
Property RootPath As String
Sub New(ByVal Name As String, ByVal RootPath As String)
MyBase.New(Name = Name)
Me.RootPath = RootPath
End Sub
End Class
Dim BaseDirectory As New mrBaseFURDirectory("A:\", "a:\") 'RAM disk full files/folders
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'setup IO
Dim IOBaseDirectory As IO.DirectoryInfo
IOBaseDirectory = New IO.DirectoryInfo(BaseDirectory.RootPath)
'go get 'em tiger!
GatherChildDirectoryStats(IOBaseDirectory, BaseDirectory)
MsgBox("Done.")
End Sub
Public Sub GatherChildDirectoryStats(ByVal IODirectory As IO.DirectoryInfo, ByVal FURDirectory As mrFURDirectory)
'get the file count and total size of files in this directory
For Each aFile As IO.FileInfo In IODirectory.GetFiles
FURDirectory.FileSize += aFile.Length
FURDirectory.FileCount += 1
Next
For Each Directory As IO.DirectoryInfo In IODirectory.GetDirectories
'DirCount likely redundant..
FURDirectory.DirCount += 1
'if we're in here, we need another child, make one
Dim NewChildDir As mrFURDirectory
'attach it to the parent
FURDirectory.Children.Add(New mrFURDirectory(Directory.Name))
'reference the child
NewChildDir = FURDirectory.Children(FURDirectory.Children.Count - 1)
'tell the child who the parent is
NewChildDir.Parent = FURDirectory
'recurse into sub again
GatherChildDirectoryStats(Directory, NewChildDir)
Next
End Sub
After adding up all the file sizes in each node you are calling the subnodes to do the same. After this call returns add this:
So the last node sums up its files, returns and the sums are added to the n-1 node etc. Recursion rocks!