Sunday, April 29, 2007

function GetDirSize (dir: string; subdir: boolean): longint;
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;
findclose(rec);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(IntToStr(GetDirSize('c:\windows', false)) + ' B'+#13+IntToStr(GetDirSize('c:\windows', true)) + ' B');
end;
Příklad vypíše velikost adresáře Windows a to jak samotného, tak včetně podadresářů.

Labels: