Get Directory Size

wengwashere

New member
Joined
Jun 24, 2003
Messages
4
hi!

1. how do i get the size of a certain directory?

2. how can i use the filesystem watcher to tell me that a certain directory size has reached a certain limit?
 
If by directory size, you mean the sum of all file sizes in a certain directory, you can use Directory.GetFiles() to get all the files w/in a directory, then iterate thru each file to accumulate the FileInfo.Length property.

On the FileSystemWatcher, I think what it can monitor is when files are added, deleted, modified, etc. but not "directory size".
 
Originally posted by JABE
If by directory size, you mean the sum of all file sizes in a certain directory, you can use Directory.GetFiles() to get all the files w/in a directory, then iterate thru each file to accumulate the FileInfo.Length property.

is there any other way than that? like getting the directory size automatically?
 
The point is there is no such thing as directory size. Only files have size. Period. Open up Windows Explorer, right click a directory with numerous files in it and select "Properties". Watch as Windows increases the "directory size", as it enumerates each file and requests its size. Youll need to use the method JABE mentioned, above.
 
Im totaly new to VB.Net and am looking to do the same thing. In VB Script it is really straight forward. You use the Scripting.FileSystemObjtect function with a call to GetFolder. Then you can just use objSubFolder.Size and it returns the directory size. Shoot an email if you want an example script. I really thought this would be a fun project in VB.Net, but I am having a hard time with it.
 
you can still add a reference to the Scripting library ( project , add reference , COM ) , then you can get the Size of a directory / folder easily without having to write a Loop. eg:
Code:
        Dim fs As New Scripting.FileSystemObject()
        Dim folder As Scripting.FolderClass = fs.GetFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal))
        MessageBox.Show(folder.Size)
 
Lol... I got well told off in here once for still using the FSO.:D

Ive learnt my lesson...

This is the way I do it now...

Code:
Dim MyFolderSize As Double = 0
Dim MyDir As New IO.DirectoryInfo(FOLDER PATH HERE)
Dim MyFilesArray As IO.FileSystemInfo() = MyDir.GetFiles
If MyFilesArray.Length > 0 Then
    Dim MyFile As IO.FileInfo
    For Each MyFile In MyFilesArray
        MyFolderSize += MyFile.Length
    Next
End If
To change it to Megabytes
MyFolderSize = Math.Round(((MyFolderSize / 1024) / 1024), 2)

See I do pay attention.

This will only total up the size of the files in the folder not the subfolders though.
 
But since you need the size of the directories in the directories:
Code:
Function FolderSize(ByVal strFolderPath As String) As Decimal
        Dim TopDir As New IO.DirectoryInfo(strFolderPath)
        Dim Dir As IO.DirectoryInfo
        Dim FilesArray As IO.FileSystemInfo()
        Dim DirSize As Decimal

        For Each Dir In TopDir.GetDirectories
            DirSize += FolderSize(Dir.FullName)
        Next Dir

        FilesArray = TopDir.GetFiles
        If FilesArray.Length > 0 Then
            Dim MyFile As IO.FileInfo
            For Each MyFile In FilesArray
                DirSize += MyFile.Length
            Next MyFile
        End If

        FolderSize = DirSize

End Function
Thanks for geting me started.
 
Last edited by a moderator:
Try this for a succinct implementation:[VB] Private Function GetFolderSize(ByVal path As String)
Add the size of each file in this folder.
For Each fileName As String In IO.Directory.GetFiles(path)
GetFolderSize += (New IO.FileInfo(fileName)).Length
Next

Add the size of each subfolder.
For Each folderName As String In IO.Directory.GetDirectories(path)
GetFolderSize += Me.GetFolderSize(folderName)
Next
End Function[/VB]
 
Isnt it somehow strange that the FileSystemWatcher class can check for changes in the size of a directory without knowing how big it really is?

Would be nice if some1 explains this.

Thx

Ash
 
Last edited by a moderator:
By the way this is how u do it in c#

U call that method with a ne DirectoryInfo(YourPath).
E.g.:

Code:
public class SomeClass()
{
  public SomeClass()
  {
    DirectoryInfo aInfo = new DirectoryInfo(_sMyDocPath);
    getDirectorySize(aInfo);
  }
}

private void getDirectorySize(DirectoryInfo diMyDocInfo)
{
  foreach(FileInfo aFileInfo in diMyDocInfo.GetFiles())
  {
    _lTotalSize += aFileInfo.Length;
  }	

  foreach (DirectoryInfo aDirInfo in diMyDocInfo.GetDirectories())
  {
    getDirectorySize(aDirInfo);
  }
}
While _sMyDocPath) is the path of your directory.
 
Back
Top