Mega Code Archive

 
Categories / Delphi / Files
 

Getdirectorysize

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 label1.Caption := FloatToStr(GetDirSize('e:\download', False) / Sqr(1024)) + ' MBytes'; label2.Caption := FloatToStr(GetDirSize('e:\download', True) / Sqr(1024)) + ' MBytes'; end;