How to get the total size of a folder?

  • Thread starter Thread starter grouped2000
  • Start date Start date
G

grouped2000

g'day group,

I need some code that will give me the total size of a folder. In the
MSDN I've found some code, but I cannot figure out how to use this code
at all ...

I am quite new to .NET, I have been using VB6 always. Can anyone point
me in the right direction?

How will the following code give me the total size of al files in
folder "c:\test" ? Where do I put this code? And how do I get the total
size out of this class and function?


thanks,
Ed
--------------------

Imports System
Imports System.IO
Imports Microsoft.VisualBasic
' the above is already at the right spot in the code



Public Class ShowDirSize

Public Shared Function DirSize(ByVal d As DirectoryInfo) As
Long
Dim Size As Long = 0
' Add file sizes.
Dim fis As FileInfo() = d.GetFiles()
Dim fi As FileInfo
For Each fi In fis

Size += fi.Length
Next fi
' Add subdirectory sizes.
Dim subs() As Boolean

'If subs(counter) = True Then

Dim dis As DirectoryInfo() = d.GetDirectories()
Dim di As DirectoryInfo
For Each di In dis
Size += DirSize(di)
Next di
Return Size

'End If

End Function 'DirSize

Public Overloads Shared Sub Main(ByVal args() As String)
If args.Length <> 1 Then
Console.WriteLine("You must provide a directory
argument at the command line.")
Else
Dim d As New DirectoryInfo(args(0))
Console.WriteLine("The size of {0} and its
subdirectories is {1} bytes.", d, DirSize(d))
End If
End Sub 'Main
End Class 'ShowDirSize
 
never mind, found some other code, much shorter, doing the job:

Private Function DirSize(ByVal path As String) As Long


Dim sz As Long = 0
Dim d As DirectoryInfo = New DirectoryInfo(path)


' get file length
Dim f As FileInfo


For Each f In d.GetFiles()
sz += f.Length
Next


' recurse into directories
Dim dx As DirectoryInfo


For Each dx In d.GetDirectories()
sz += DirSize(dx.FullName)
Next


Return sz


End Function
 
Grouped,

You can better use the Integer as indexer in VBNet, that it for the 32Bit
computer the most optimize one.

I hope this helps,

Cor
 
Hi,

But the Intereger data type only has a limited range.. and when calculating
direcotry sizes the long data type is better as its range is larger
(Specialy for larger directories..)

Greetz
Lon
 
Lon,
But the Intereger data type only has a limited range.. and when
calculating direcotry sizes the long data type is better as its range is
larger
(Specialy for larger directories..)
Interesting, can you tell us how many files that there can be in a directory
and how much you can index with an integer and how much with a long?

Cor
 
Cor Ligthert said:
Interesting, can you tell us how many files that there can be in a
directory and how much you can index with an integer and how much with a
long?

When summing up file sizes, I would use a 'Long' too. Maybe 'ULong' would
be preferrable because it can deal with larger numbers.
 
Given the fact that FileInfo.Length returns a long, I would suggest he sum
it up in a long.
 
Herfried,

I was talking about the indexer to count the files.

You probably don't believe it, I think I know the value that an integer
posivite and negatieve can contain.

With the integer is possible more than 4.000.000.000 which is a lot in of
files in one folder in my opinion.

I was not talking about file size.

However if you want to say that for this kind of cound indexers better a
long can be used than am I really curious for your explanation.

Cor
 
Anon,

You are right, I misreaded it, however my advice was to use for indexers an
integer.

And your message was connected to mine, not to somebody else.

Cor
 
Back
Top