Gets the size of the folder with all its contents.
1 2 3 4 5 6 7 8 9 10 11 12 |
var path: string; incSubDirs: boolean; size: int64; begin path:= 'C:\Program Files'; incSubDirs:= true; size:= 0; getDirSize(path, incSubDirs, size); ShowMessage(IntToStr(size)); end; |
Options
path - path to the folder (you can either with a slash at the end or without)
incSubDirs - whether or not to include subdirectories in the count
size - variable for displaying the result of the procedure
Result
The size of the folder in bytes.
If the path does not exist, the state of the size variable will remain 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
procedure getDirSize(dir: string; incSubDirs: Boolean; var Result: Int64); var SearchRec: TSearchRec; FindResult: int64; begin Dir := IncludeTrailingBackslash(Dir); FindResult := FindFirst(Dir + '*.*', faAnyFile, SearchRec); try while FindResult = 0 do with SearchRec do begin if (Attr and faDirectory) <> 0 then begin if IncSubDirs and (Name <> '.') and (Name <> '..') then getDirSize(Dir + Name, IncSubDirs, Result); end else begin Result := Result + (Size); end; FindResult := FindNext(SearchRec); end; finally FindClose(SearchRec); end; end; |