Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Visual Basic (Classic) FAQ

Folders and Files

How To Find The Size Of A Folder and All Its SubFolders by mmilan
Posted: 11 Jun 04

Bit of code for anyone who wants it - I wrote this for finding the size of a folder and all it's subfolders.


Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        nFileSizeHigh As Long
        nFileSizeLow As Long
        dwReserved0 As Long
        dwReserved1 As Long
        cFileName As String * 255
        cAlternate As String * 14
End Type
Private Const ERROR_NO_MORE_FILES = 18&

Private Function FolderSize(sFolder) As Double
  Dim FindInfo As WIN32_FIND_DATA
  Dim lFindHandle As Long
  Dim lTemp As Long
  Dim dSize As Double
  Dim bNextLoop As Boolean
  Dim sFileName As String
  'Open up an API FIND on the folder.
  lFindHandle = FindFirstFile(sFolder + "\*", FindInfo)
  If lFindHandle <> INVALID_HANDLE_VALUE Then
      sFileName = FindInfo.cFileName
      sFileName = Replace(sFileName, Chr(0), " ")
      sFileName = Trim(sFileName)
      If sFileName <> "." And sFileName <> ".." Then
          'We're on a folder.
          dSize = dSize + FolderSize(sFolder + "\" + sFileName)
          'We're on a file.
          dSize = dSize + FindInfo.nFileSizeLow
          dSize = dSize + (FindInfo.nFileSizeHigh * (2 ^ 16))
        End If
      End If
      FindInfo.cFileName = String(255, vbNullChar)
      lTemp = FindNextFile(lFindHandle, FindInfo)
      bNextLoop = (lTemp <> ERROR_NO_MORE_FILES) And (lTemp <> 0)
    Loop While bNextLoop
    'Close down the file handle
    FindClose lFindHandle
  End If

  FolderSize = dSize

End Function

Back to Visual Basic (Classic) FAQ Index
Back to Visual Basic (Classic) Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close