本文整理自网络,侵删。
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;
/////////////////////////////////////////////////////////////////////////////////////////
function GetFolderSize2(vFolder: String): Int64;
var
sr: TSearchRec;
begin
Result := 0;
if FindFirst(vFolder + '*.*', faAnyFile, sr) = 0 then
repeat
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
Result := Result + sr.Size;
if (sr.Attr and faDirectory) <> 0 then
Result := Result + GetFolderSize(vFolder + sr.Name + '\');
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
------------------------------------
调用ShowMessage(IntToStr(GetFolderSize('e:\')));//注意参数最后一个字母应该为'\'
相关阅读 >>
Delphi pchar和array [0..255] of char的区别
Delphi dll注入x86/x64/win2k~win8.1全可用
Delphi access数据库密码的mdb的访问报错“无法启动应用程序,或是已被其他用户已独占方式打开”
Delphi setformfullscreen()窗体全屏显示
更多相关阅读请进入《Delphi》频道 >>