使用下面的函数,它使用递归算法查找所有属性的文件(包括隐藏、系统、存档等)在该文件夹及子文件夹。
仅仅使用一个文件夹路径作为参数。
Try the following function (it looks at hidden, system, archive, and normal files; it uses a recursive algorithm to look in all sub-directories also; just supply a starting directory as a parameter.Copy From:http://www.qfly.cn
[separater]
[quote]
var
DirBytes : integer;
function FolderSize(Dir:string):integer;
var
SearchRec : TSearchRec;
Separator : string;
begin
Result:=0;
if Copy(Dir,Length(Dir),1)='/' then
Separator := ''
else
Separator := '/';
if FindFirst(Dir+Separator+'*.*',faAnyFile,SearchRec) = 0 then begin
if FileExists(Dir+Separator+SearchRec.Name) then begin
DirBytes := DirBytes + SearchRec.Size;
end else if DirectoryExists(Dir+Separator+SearchRec.Name) then begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then begin
FolderSize(Dir+Separator+SearchRec.Name) ;
end;
end;
while FindNext(SearchRec) = 0 do begin
if FileExists(Dir+Separator+SearchRec.Name) then begin
DirBytes := DirBytes + SearchRec.Size;
end else if DirectoryExists(Dir+Separator+SearchRec.Name) then
begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then begin
FolderSize(Dir+Separator+SearchRec.Name) ;
end;
end;
end;
end;
FindClose(SearchRec) ;
Result:=DirBytes;
end;
[/quote]