Directory Size

Windows specific forum
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Try this :

Code: Select all

;/ Return Size of a Directory ( in Kb )

Procedure SearchDirectorySize(Path.s)
  
  ; Add \ to Path if missing
  If Right(Path,1)<>"\" : Path+"\":EndIf
  
  ; Apply Structure
  lpFindFileData.WIN32_FIND_DATA
  
  ; Add Filter *.*
  Recherche.s=Path+"*.*"
  
  ; Initiate the Search
  handle.l = FindFirstFile_(Recherche, @lpFindFileData)
  
  ; If search succeeds
  If handle <> #INVALID_HANDLE_VALUE
    
    Repeat
      
      ; Trouve = File or Directory Found
      Trouve.s=PeekS(@lpFindFileData\cFileName)
      
      ; This is a not a directory
      If lpFindFileData\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY =#False
        
        Fichiers.s=Path+Trouve
        
        Size+( lpFindFileData\nFileSizeLow / 1024) ; Add Low DWord
        If lpFindFileData\nFileSizeHigh
          Size+lpFindFileData\nFileSizeHigh * $3FFFFF ; Add High DWord *$3FFFFF
        EndIf
        
      EndIf
      
      ; Exit when there is no more files
    Until FindNextFile_(handle, @lpFindFileData)= #False
    
    ; Close the Api search Function
    FindClose_(handle)
    
  EndIf

  ProcedureReturn Size
  
EndProcedure
  
Procedure SearchSubDirectorySize(Path.s)
  
  ; Add \ to Path if missing
  If Right(Path,1)<>"\" : Path+"\":EndIf
  
  ; Apply Structure
  lpFindFileData.WIN32_FIND_DATA
  
  ; Add Filter *.*
  Recherche.s=Path+"*.*"
  
  ; Initiate the Search
  handle.l = FindFirstFile_(Recherche, @lpFindFileData)
  
  ; If search succeeds
  If handle <> #INVALID_HANDLE_VALUE
    
    Repeat
      
      ; trouve = File Or Directory Found
      Trouve.s=PeekS(@lpFindFileData\cFileName)
      
      ; This is a directory
      If lpFindFileData\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY
        
        ; And not the . or .. directory
        If Trouve <>"." And Trouve <>".."
          
          ; Call the function itself ( Recursive ) to search in another Directory
          Size+SearchSubDirectorySize(Path+Trouve)
          
          ; Directory found : Search file within this Directory
          Size+ SearchDirectorySize(Path+Trouve)
          
        EndIf
        
      EndIf
      
      ; Exit when there is no more files
    Until FindNextFile_(handle, @lpFindFileData)= #False
    
    ; Close the Api search Function
    FindClose_(handle)
    
  EndIf

  ProcedureReturn Size
  
EndProcedure

ProcedureDLL GetDirectorySize(Path.s) 
  
  Size=SearchDirectorySize(Path) ; Car le répertoire lui même n'est pas scanné sinon
  Size+SearchSubDirectorySize(Path)
  
  ProcedureReturn Size
  
EndProcedure

;/ Test
#Directory="c:\Windows\"
MessageRequester(#Directory,Str(GetDirectorySize(#Directory))+" Kb")
trather
User
User
Posts: 29
Joined: Sun Nov 07, 2004 9:57 pm

Post by trather »

That works great. That is what I was looking for. Sorry it took me so long to get back to you but life got in the way of programing. 8)
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

No problemo 8)
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Very nice..

- np

** oops .. you have the package somewhere in non-installer form?
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post by NoahPhense »

Thanks man.. appreciated..

- np
Post Reply