function GetDirectorySize(const ADirectory: string): Integer;
var
Dir: TSearchRec;
Ret: integer;
Path: string;
begin
Result := 0;
Path := ExtractFilePath(ADirectory);
Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);
if Ret<>0 then
begin
ShowMessage('路径输入错误');
exit;
end;
try
while ret=0 do
begin
inc(Result, Dir.Size);
if (Dir.Attr in [faDirectory]) and (Dir.Name[1] <> '.') then
Inc(Result, GetDirectorySize(Path + Dir.Name + '\*.*'));
Ret := Sysutils.FindNext(Dir);
end;
finally
Sysutils.FindClose(Dir);
end;
end;
示范性代码
不一定编译得过
要修改
var
temprec : tseachrec;
size : LogInt;
begin
size := 0;
if findfist then
repeat
size := size + temprec.size;
if temprec.fa = 目录 then
begin
callself(startdir + '\' + temprec.name) //调用自己,递归
findfirst(temprec.name);
end;
until findnext;
result := size;
end;