There are those times when you would like to know the size of a specific directory. To accomplish this in Delphi XE8 simply use the code block below.
[codesyntax lang=”delphi”]
{*----------------------------------------------------------------------------- Procedure: GetDirSize Author: archman Date: 21-May-2015 @Param dir: string; subdir: Boolean @Return Int64 -----------------------------------------------------------------------------} function TBCSDirSizeC.GetDirSize(dir: string; subdir: Boolean): Int64; var rec: TSearchRec; found: Integer; begin Result := 0; if dir[Length(dir)] <> '\' then dir := dir + '\'; found := FindFirst(dir + '*.*', faAnyFile, rec); while found = 0 do begin Inc(Result, rec.Size); if (rec.Attr and faDirectory > 0) and (rec.Name[1] <> '.') and (subdir = True) then Inc(Result, GetDirSize(dir + rec.Name, True)); found := FindNext(rec); end; System.SysUtils.FindClose(rec); end;
[/codesyntax]
Here is a Delphi XE8 code snippet that show how to populate the list box.
[codesyntax lang=”delphi”]
{*----------------------------------------------------------------------------- Procedure: LoadListBox Author: Mr. Arch Brooks, Software Engineer, Brooks Computing Systems LLC Date: 04-Jun-2015 @Param None @Return None -----------------------------------------------------------------------------} Procedure TBCSMoveDirC.LoadListBox; var rdir : string; obuf : string; begin dlgSelDir.Title := 'Select Source Directory'; if dlgSelDir.Execute then begin dc := TDirectory.GetDirectories(dlgSelDir.FileName, '*', TSearchOption.soTopDirectoryOnly); iend := Length(dc); i := 0; repeat rdir := dc[i]; obuf := FormatFloat('#,##0.00', GetDirSize(rdir, true) / (1024 * 1024)); lbxDirs.Items.Add(obuf + ' MB ' + rdir); Inc(i); until (i > (iend - 1)); dlgSelDir.Title := 'Select Destination Directory'; if Not dlgSelDir.Execute then lbxDirs.Items.Clear; end; end;
[/codesyntax]
The output from the block of code is depicted below.
This view allows you to review the size of the associated directories. I typically like to perform operations on the largest directories so I can easily free up the maximum amount of disk space.
To remove the size specification from the directory invoke these following two statements.
[codesyntax lang=”delphi”]
obuf := lbxDirs.Items[i];
Delete(obuf, 1, Pos(‘ MB ‘, obuf) + 3);
[/codesyntax]
Once the directory specification is restored processing may resume normally.
Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.