Results 1 to 3 of 3

Thread: Size

  1. #1

    Thread Starter
    Member
    Join Date
    May 2001
    Location
    Malaysia
    Posts
    43

    Cool Size

    Is there any API that can get the size of directory? I found GetFileSize. Does it applies for directory? Any example? Thanks!
    Ben Chin

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    You can loop through the files in the directory and get the size of each file.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Code:
    Option Explicit
    
    Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
    Private Const MAXDWORD = &HFFFF
    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 * 260
      cAlternate        As String * 14
    End Type
    
    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
    
    Dim bytes As Double
    
    Public Function NuFolderSize(ByVal Path As String, Optional IncludeSubdirectories = False) As Double
     'Nucleus
     NuFolderSize = TotalBytes(Path, IncludeSubdirectories)
     bytes = 0
    End Function
    
    Private Function TotalBytes(ByVal Path As String, Optional IncludeSubdirectories = False) As Double
     Dim hfind&, fname$
     Dim WFD As WIN32_FIND_DATA
     Dim found As Boolean
        
     If Right(Path, 1) <> "\" Then Path = Path & "\"
     If Len(Dir$(Path, vbDirectory)) <> 0 Then
        hfind = FindFirstFile(Path & "*", WFD)
        found = (hfind > 0)
        While found
        fname = Left$(WFD.cFileName, InStr(WFD.cFileName, Chr$(0)) - 1)
        If fname <> "." And fname <> ".." Then
            If WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY Then
                If IncludeSubdirectories Then TotalBytes Path & fname, IncludeSubdirectories
            Else
                bytes = bytes + WFD.nFileSizeHigh * MAXDWORD + WFD.nFileSizeLow
            End If
        End If
        found = FindNextFile(hfind, WFD)
        Wend
     End If
     FindClose hfind
     TotalBytes = bytes
    End Function
    Code:
    Private Sub Command1_Click()
        'comparison between fso And nuFolderSize
        'Need To Set a reference To microsoft scripting runtime
        Dim FSO As Scripting.FileSystemObject
        Dim folder As Scripting.folder
        Dim dr As Scripting.Drive
        Set FSO = New Scripting.FileSystemObject
        Set folder = FSO.GetFolder("d:\")
        
        'FSO folder size
        MsgBox Format(folder.Size / 1024, "#,#") & "KB"
    
        'NuFolderSize
        MsgBox Format(NuFolderSize("d:\", True) / 1024, "#,#") & "KB"
        
        'FSO TotalDriveSize
        Set dr = FSO.GetDrive("d")
        MsgBox Format(dr.TotalSize / 1024, "#,#") & "KB"
        Set FSO = Nothing
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width