⤷ Find out total size of directory

You should specify a directory. After that, you should find all files in this directory by recursion. You should add to the total size the size of each found file.

procedure TForm1.Find(Str: string);
var
  MySearch: TSearchRec;
  FindResult: Integer;
begin
  FindResult:=FindFirst(Str+'\*.*', 
                        faArchive+faHidden+
                        faAnyFile+faVolumeID+
                        faSysFile+faReadOnly+faDirectory,
                        MySearch);
  while FindResult=0 do
  begin
    if (MySearch.Attr=faDirectory) and 
       (MySearch.Name<>'.') and 
       (MySearch.Name<>'..') then
      Find(Str+'\'+MySearch.Name)
    else
      TotalSize:=TotalSize+MySearch.Size;
    FindResult:=FindNext(MySearch);
  end;
  FindClose(MySearch);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TotalSize:=0;
  if Length(Edit1.Text)>0 then
  begin
    Find(Edit1.Text);
    Label2.Caption:='Total size = '+IntToStr(TotalSize);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  St: string;
begin
  St:='c:\';
  if SelectDirectory(St, [], 0) then
    Edit1.Text:=St;
end;
More for developers