Resolved Need help to sum up directories

Tommyknocker

Member
Joined
Jul 27, 2009
Messages
5
Programming Experience
Beginner
Hello,
I'm beginner in VB and I try to sum up directories file sizes.
I managed to get a piece of code that worked, but not the way I want.
This code, so far, get the size of the files in a directory and sum up their size; but if there are subdirectories, it only give the size of each individual directories.
I would like to have the total size of the top folder and subdirs.
Here is the code. If someone could help me, it would be great.

VB.NET:
    Private Function RecursiveSize(ByVal path As String) As Boolean
        Dim dirInfo As New IO.DirectoryInfo(path)
        Dim fileObject As FileSystemInfo
        Dim size_reader, size_file, size_directory, size_compressed, size_non_compressed, size_directory_cf, size_directory_ncf As Long
        Dim sdir, srep As New DirectoryInfo(path)
        Try
            For Each fileObject In dirInfo.GetFileSystemInfos()
                If (fileObject.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
                    RecursiveSearch(fileObject.FullName)
                Else
                    size_reader = GetCompressedFileSize(path, 0)
                    size_compressed = Nothing
                    size_non_compressed = Nothing
                    If fileObject.Attributes = "2080" Then
                        size_file = GetCompressedFileSize(fileObject.FullName, 0)
                        size_compressed = size_file - size_reader
                    Else
                        Dim sfile As FileInfo = fileObject
                        size_non_compressed = (Format(sfile.Length / 4096, "##0") * 4096 + 4096)
                    End If
                    size_directory_cf += size_compressed
                    size_directory_ncf += size_non_compressed
                End If
            Next
            size_directory = size_directory_cf + size_directory_ncf - 4096
            If size_directory > "9999999999" Then
                Console.WriteLine(Format((size_directory / 1024 / 1024 / 1024), "##,###.0") & " GB")
            ElseIf size_directory > "9999999" Then
                Console.WriteLine(Format((size_directory / 1024 / 1024), "##,###.0") & " MB")
            ElseIf size_directory > "9999" Then
                Console.WriteLine(Format((size_directory / 1024), "##,###.0") & " KB")
            ElseIf size_directory > "9" Then
                Console.WriteLine(Format((size_directory), "##,###.0") & " Bytes")
            End If
            Return True
            Exit Function
        Catch ex As Exception
            MsgBox("Error")
        End Try
    End Function

Thanks in advance
 
Last edited:
Hello.

You mean something like this?

VB.NET:
    Public Shared Function RecursiveSize(ByVal sourceFolder As String, Optional ByVal alsoGetSubFolders As Boolean = True) As Long
        Dim sizeSum As Long = 0

        If Directory.Exists(sourceFolder) Then
            If alsoGetSubFolders Then
                For Each foldername As String In Directory.GetDirectories(sourceFolder)
                    sizeSum += RecursiveSize(Path.Combine(sourceFolder, foldername), True)
                Next
            End If

            For Each filename As String In Directory.GetFiles(sourceFolder)
                sizeSum += New FileInfo(Path.Combine(sourceFolder, filename)).Length
            Next
        Else
            RecursiveSize = sizeSum
        End If
    End Function

Bobby
 
Hi Bobby.
Thanks for the reply, but I end up with the same result.
Lets take an example. I have a directory called "test" with files for 5 MB. In this "test" dir, I have a subdir called "subtest" with files for 7 MB.
With your solution (much more cleaner than mine), I end up with a value of 5MB for the first dir and a value of 7 MB for the second lin. If I add a msgbox between the "End If" and the "End Function"; I will have 1 box for the 1st dir and 1 box for the 2nd.
I would like to end up with a single box where it shows 12 MB.

Thanks in advance.
 
I'm sorry man, I somehow screwed that up, please try this:

VB.NET:
    Public Shared Function RecursiveSize(ByVal sourceFolder As String, Optional ByVal alsoGetSubFolders As Boolean = True) As Long
        Dim sizeSum As Long = 0

        If Directory.Exists(sourceFolder) Then
            If alsoGetSubFolders Then
                For Each foldername As String In Directory.GetDirectories(sourceFolder)
                    sizeSum += RecursiveSize(Path.Combine(sourceFolder, foldername), True)
                Next
            End If

            For Each filename As String In Directory.GetFiles(sourceFolder)
                sizeSum += New FileInfo(Path.Combine(sourceFolder, filename)).Length
            Next
        End If

        RecursiveSize = sizeSum
    End Function

Well, try calling it like this where you want to show the messagebox:
VB.NET:
MessageBox.Show("Size of """ & "yourPath" & """: " & RecursiveSize("yourPath").ToString("N0") & " Bytes", "Size", MessageBoxButtons.OK)

Bobby
 
With DirectoryInfo.GetFiles(String, SearchOption) method you can get all FileInfos including in subdirs, use SearchOption.AllDirectories.
 
Back
Top