I have here two versions, one specific to Windows, another using PB native commands.
{ Windows Specific }
Code: Select all
Structure LARGE_INTEGER_
StructureUnion
x.LARGE_INTEGER
QuadPart.q
EndStructureUnion
EndStructure
Procedure.q GetFolderSize(Path.s, State.b = 1)
Protected _Data.WIN32_FIND_DATA, Path$ = Path + "\*.*"
Static.q nSize
If State : nSize = 0 : EndIf
hF = FindFirstFile_(Path$, @_Data)
If hF <> #INVALID_HANDLE_VALUE
Repeat
If _Data\dwFileAttributes & #FILE_ATTRIBUTE_DIRECTORY
;// skip file or directory that has an associated reparse point,
;// or a file that is a symbolic link. Basically to avoid potential
;// infinitely recursive directory tree.
If Not _Data\dwFileAttributes & #FILE_ATTRIBUTE_REPARSE_POINT
;// make sure we skip "." And ".."
If PeekS(@_Data\cFileName) <> "." And PeekS(@_Data\cFileName) <> ".."
;// We found a sub-directory, so get the files in it too
Path$ = Path + "\" + PeekS(@_Data\cFileName)
;// recurrsion here!
GetFolderSize(Path$, 0) ; Traverse Directory
EndIf
EndIf
Else
sz.LARGE_INTEGER_
;// All we want here is the file size. Since file sizes can be larger
;// than 2 gig, the size is reported As two DWORD objects. Below we
;// combine them To make one 64-bit integer.
sz\x\LowPart = _Data\nFileSizeLow
sz\x\HighPart = _Data\nFileSizeHigh
nSize + sz\QuadPart
EndIf
Until FindNextFile_(hF, @_Data) = 0
FindClose_(hF)
EndIf
ProcedureReturn nSize
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Path.s = #PB_Compiler_Home
Debug #CRLF$+"GetFolderSize()"
Debug "Path: " + Path
Debug " "+Str(GetFolderSize(Path)) + " Bytes"
CompilerEndIf
Code: Select all
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
#PB_FileSystem_Link = #FILE_ATTRIBUTE_REPARSE_POINT
CompilerEndIf
Procedure.q GetFolderSize(Path.s, State.b = 1)
Protected Path$ = Path
Static.q nSize
If State : nSize = 0 : EndIf
hF = ExamineDirectory(#PB_Any, Path, "*.*")
If hF <> #False
While NextDirectoryEntry(hF)
If DirectoryEntryType(hF) = #PB_DirectoryEntry_Directory
If DirectoryEntryName(hF) <> "." And DirectoryEntryName(hF) <> ".."
If DirectoryEntryAttributes(hF) & #PB_FileSystem_Link
Continue
EndIf
Path$ = Path + "\" + DirectoryEntryName(hF)
GetFolderSize(Path$, 0) ; Traverse Directory
EndIf
Else
nSize + FileSize(Path + "\" + DirectoryEntryName(hF))
EndIf
Wend
FinishDirectory(hF)
EndIf
ProcedureReturn nSize
EndProcedure
CompilerIf #PB_Compiler_IsMainFile
Path.s = #PB_Compiler_Home
Debug #CRLF$+"GetFolderSize()"
Debug "Path: " + Path
Debug " "+Str(GetFolderSize(Path)) + " Bytes"
CompilerEndIf