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.
Thanks in advance
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: