Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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".

Posted
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?

  • *Gurus*
Posted
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. You'll need to use the method JABE mentioned, above.
  • 4 months later...
Posted
I'm 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.
  • Leaders
Posted

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:

       Dim fs As New Scripting.FileSystemObject()
       Dim folder As Scripting.FolderClass = fs.GetFolder(Environment.GetFolderPath(Environment.SpecialFolder.Personal))
       MessageBox.Show(folder.Size)

Posted

Lol... I got well told off in here once for still using the FSO.:D

 

I've learnt my lesson...

 

This is the way I do it now...

 

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.

I'm getting the hang of this now... No really I am!
  • 3 weeks later...
Posted (edited)

But since you need the size of the directories in the directories:

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.

Edited by btoniob
  • 1 year later...
Posted
Try this for a succinct implementation:
    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

  • 6 months later...
Posted (edited)

Isn't 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

Edited by Asharon
Posted

By the way this is how u do it in c#

 

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

E.g.:

 

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...