iNET Interactive - Online Advertising Agency
          
Go Back  Xtreme .NET Talk > .NET > Directory / File IO / Registry > Get Directory Size


Reply
 
Thread Tools Display Modes
  #1  
Old 07-09-2003, 01:01 AM
wengwashere wengwashere is offline
Newcomer
 
Join Date: Jun 2003
Posts: 4
Default Get Directory Size

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?
__________________
wEnGwAsHeRe
Reply With Quote
  #2  
Old 07-09-2003, 01:12 AM
JABE JABE is offline
Junior Contributor

Preferred language:
VB.NET
 
Join Date: Jun 2003
Location: Philippines
Posts: 375
Default

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".
Reply With Quote
  #3  
Old 07-09-2003, 01:49 AM
wengwashere wengwashere is offline
Newcomer
 
Join Date: Jun 2003
Posts: 4
Default

Quote:
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?
__________________
wEnGwAsHeRe
Reply With Quote
  #4  
Old 07-09-2003, 03:35 PM
Derek Stone's Avatar
Derek Stone Derek Stone is offline
Exalted One

Preferred language:
C#
 
Join Date: Nov 2002
Location: Rhode Island, USA
Posts: 1,878
Default

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.
__________________
Posting Guidelines
Reply With Quote
  #5  
Old 11-14-2003, 11:24 AM
sschradle sschradle is offline
Newcomer
 
Join Date: Nov 2003
Posts: 1
Default

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.
Reply With Quote
  #6  
Old 11-17-2003, 05:50 AM
dynamic_sysop's Avatar
dynamic_sysop dynamic_sysop is offline
Senior Contributor

Preferred language:
C#
 
Join Date: Oct 2002
Location: Ashby, Leicestershire.
Posts: 1,039
Default

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)
Reply With Quote
  #7  
Old 11-17-2003, 04:05 PM
Reapz Reapz is offline
Regular

Preferred language:
VB.NET
 
Join Date: Dec 2002
Posts: 74
Default

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

I've 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.
__________________
I'm getting the hang of this now... No really I am!
Reply With Quote
  #8  
Old 12-03-2003, 10:38 PM
btoniob btoniob is offline
Newcomer
 
Join Date: Dec 2003
Posts: 1
Default

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 btoniob : 12-03-2003 at 10:58 PM.
Reply With Quote
  #9  
Old 07-01-2005, 10:21 PM
adammendoza adammendoza is offline
Newcomer
 
Join Date: Jul 2005
Location: California, USA
Posts: 1
Default Freaking sweet!!

Sweet code
Reply With Quote
  #10  
Old 07-03-2005, 09:53 PM
jmcilhinney jmcilhinney is offline
Centurion
 
Join Date: Jun 2005
Posts: 110
Default

Try this for a succinct implementation:
Code:
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
Reply With Quote
  #11  
Old 01-19-2006, 10:37 AM
Asharon Asharon is offline
Newcomer
 
Join Date: Jan 2006
Posts: 5
Default

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

Last edited by Asharon : 01-19-2006 at 10:49 AM.
Reply With Quote
  #12  
Old 01-23-2006, 04:54 AM
Asharon Asharon is offline
Newcomer
 
Join Date: Jan 2006
Posts: 5
Default

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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Active Directory... on what base does LDAP connect to Active Directory RTT ASP.NET 0 04-12-2005 11:20 AM
obtaining the size of a directory object inzo21 Directory / File IO / Registry 2 01-11-2004 03:09 PM
How to tell if a directory/file is a system directory/file?? sj1187534 Directory / File IO / Registry 8 07-01-2003 03:33 PM
Finding the size of an online directory Darren66 Directory / File IO / Registry 3 06-30-2003 06:38 AM
File Size oafc0000 Directory / File IO / Registry 2 03-02-2003 08:20 AM

Advertisement: