locked
finding directory size in vb.net RRS feed

  • Question

  • I've been trying to find the size of a directory with vb.net but not having much luck.

    I've tried things like:

    My.Computer.FileSystem.GetDirectoryInfo(basePath).

    but no appropriate methods or properties are coming up.

    Any ideas?

    Thanks.

    Thursday, December 29, 2005 9:45 PM

Answers

  • Private Overloads Function DirectorySize(ByVal sPath As String, ByVal bRecursive As Boolean) As Long

    Dim Size As Long = 0

    'Dim diDir As New DirectoryInfo(sPath)

    Try

    Dim fil As FileInfo

    For Each fil In diDir.GetFiles()

    Size += fil.Length

    Next fil

    If bRecursive = True Then

    Dim diSubDir As DirectoryInfo

    For Each diSubDir In diDir.GetDirectories()

    Size += DirectorySize(diSubDir.FullName, True)

    lngNumberOfDirectories += 1

    Next diSubDir

    End If

    Return Size

    Catch ex As System.IO.FileNotFoundException

    ' File not found. Take no action

    Catch exx As Exception

    ' Another error occurred

    Return 0

    End Try

    End Function

    Tuesday, January 3, 2006 6:48 PM
    Moderator

All replies

  •  

    My intuition is telling me something. I dont think you're going to be able to  just call a property a receive a number. I think you're going to need to walk the director and get a sum.

    Thursday, December 29, 2005 11:39 PM
  • that's what i'm finding...

    funny that vbscript has a size property that works so well

    Friday, December 30, 2005 5:58 PM
  •  

     

    I'll see what I can do for you.... ;)

    Friday, December 30, 2005 9:50 PM
  • Great, everything I've read so far points to a need to recursively get file lengths and sum them.
    Tuesday, January 3, 2006 4:44 PM
  • Private Overloads Function DirectorySize(ByVal sPath As String, ByVal bRecursive As Boolean) As Long

    Dim Size As Long = 0

    'Dim diDir As New DirectoryInfo(sPath)

    Try

    Dim fil As FileInfo

    For Each fil In diDir.GetFiles()

    Size += fil.Length

    Next fil

    If bRecursive = True Then

    Dim diSubDir As DirectoryInfo

    For Each diSubDir In diDir.GetDirectories()

    Size += DirectorySize(diSubDir.FullName, True)

    lngNumberOfDirectories += 1

    Next diSubDir

    End If

    Return Size

    Catch ex As System.IO.FileNotFoundException

    ' File not found. Take no action

    Catch exx As Exception

    ' Another error occurred

    Return 0

    End Try

    End Function

    Tuesday, January 3, 2006 6:48 PM
    Moderator
  • I can't believe that there isn't a simple expression that gets de directory size!
    Friday, February 17, 2006 2:09 PM
  • Its not really that suprising.  I think that is what windows does in the background when you open a folder properties window.  You'll notice that when you open the properties of a large folder such as Program Files, the folder size will steadily increase as it iterates through the folder contents and sums up the sizes.  On my PC, this takes it a while.  If vb.net did provide a simple method that would return the folder size, it would potentially hang your app for several seconds or more.  Not a good thing.
    Friday, June 22, 2007 7:53 PM
  • I think if you use

    DirectoryInfo.GetFileSystemInfos() it will return an array of FileSystemInfo and if you do a length of that array it will give you # of file in it.

    Dim drInfo As DirectoryInfo = New DirectoryInfo("D:\mytest")
    Dim sourceFolderSize() As FileSystemInfo

    sourceFolderSize = drInfo.GetFileSystemInfos()

    Console.WriteLine(sourceFolderSize.Length.ToString())

    • Proposed as answer by Willaraz Thursday, August 19, 2010 4:36 PM
    Friday, June 19, 2009 8:04 PM
  •  

    Dim drInfo As New DirectoryInfo("C:\WINDOWS")
    Dim filesInfo() As FileInfo = drInfo.GetFiles("*.*", SearchOption.AllDirectories)

     

    Dim fileSize As Long = 0

     

    For Each fileInfo As FileInfo In filesInfo

     fileSize += fileInfo.Length

     

    Next

    Console.WriteLine(fileSize.ToString())

     

    This is all you need.

    • Proposed as answer by DiMethol Tuesday, July 7, 2009 9:34 PM
    Tuesday, July 7, 2009 9:33 PM
  • What's the name for people that resurrect dead threads?
    Tuesday, July 7, 2009 10:02 PM
  • 4 year old thread, nice...

    Hector Minaya
    Microsoft Visual Basic MVP | Speaker INETA Latam | MCSD | MCT | MCTS : SQL Server
    twitter: http://twitter.com/HectorMinaya
    Looking for SEO Tools?

     

    Wednesday, July 8, 2009 12:46 AM
  • Necromancer.

    BTW, just what I needed. Into my TextPad Clipbook it goes.

     

    Thanks, 


    Mitch Bird

    Tuesday, May 25, 2010 3:43 PM
  • They are called "People Seeking Knowledge"

    I search threads all the time and if they are dead why are they found on search engines and searching on Visual Basic Developer Center 

    If you don't like it delete the thread or shut up! You didn't contribute anything to this discussion except the fact now everyone knows you're a whiner.

     Imports System.IO
    Private Function GetFolderSize(ByVal dPath As String, ByVal includeSubFolders As Boolean) As Long
    
     ' dPath: Path of the directory
     ' includesubfolders: set if include subfolders ;)
    
     Try
      Dim size As Long = 0
      Dim diBase As New DirectoryInfo(dPath)
      Dim files() As FileInfo
      If includeSubFolders Then
      files = diBase.GetFiles("*", SearchOption.AllDirectories)
      Else
      files = diBase.GetFiles("*", SearchOption.TopDirectoryOnly)
      End If
      Dim ie As IEnumerator = files.GetEnumerator
      While ie.MoveNext
      size += DirectCast(ie.Current, FileInfo).Length
      End While
      Return size
     Catch ex As Exception
      MsgBox("Error: " & ex.Message)
      Return -1
     End Try
     End Function

    Works in vb.net 2008

    Here's another way

    Public Function FldrSize(ByVal dPath As String)
    
        Dim size As Integer = 0
    
    
        For Each foundFile As String In My.Computer.FileSystem.GetFiles(dPath, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
          Dim fInfo As New FileInfo(foundFile)
          size += fInfo.Length
        Next
    
    
        Return size
    
      End Function
    

     

     

     

    • Edited by Ken6 Wednesday, June 30, 2010 5:18 AM Just because I like dead threads
    • Proposed as answer by Ken6 Wednesday, June 30, 2010 5:41 AM
    Wednesday, June 30, 2010 5:16 AM
  • "It turns out your friend here is only mostly dead."

    Thanks, Ken6.


    dj
    Wednesday, July 7, 2010 7:43 PM
  • Ken6

    Thanks for keeping threads alive!! This helped me tremendously!

    Thursday, December 13, 2012 2:02 PM