function GetDirSize(Path: String): integer;
var
SRec: TSearchRec;
FSize: integer;
begin
FSize := 0;
FillChar(SRec, SizeOf(TSearchRec), 0);
Path := AddSlash(Path);
if FindFirst(Path + '*.*', faAnyFile, SRec) = 0 then
begin
Repeat
if (SRec.Attr and faDirectory) = 0 then
FSize := FSize + SRec.Size;
Until FindNext(SRec) <> 0;
FindClose(SRec);
end;
Result := FSize;
end;上面的函数是取得一个目录下所有文件大小之和的。你可参照其中的有关语句,来取得单个文件的大小
var
SRec: TSearchRec;
FSize: integer;
begin
FSize := 0;
FillChar(SRec, SizeOf(TSearchRec), 0);
Path := AddSlash(Path);
if FindFirst(Path + '*.*', faAnyFile, SRec) = 0 then
begin
Repeat
if (SRec.Attr and faDirectory) = 0 then
FSize := FSize + SRec.Size;
Until FindNext(SRec) <> 0;
FindClose(SRec);
end;
Result := FSize;
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做了某些方面的限制。