示范性代码
不一定编译得过
要修改
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;
不一定编译得过
要修改
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;
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;上面是用递归的算法求出文件夹的大小。一般的文件夹返回的结果是正确的。但在Windows 2000下存在一些问题:
1. 当路径为 ...\winnt, ...\My Documents, 或包含了系统文件夹、系统文件的文件夹时返回的值总是小,有时能小几百兆
2. 当路径为 ...\Program Files时,返回的值干脆是 0怀疑是Windows 2000做了某些方面的限制。