我随手写的一个,没经过严格测试:function GetFileNum(dir:string):Integer; var sr:TSearchRec; Found:Integer; begin Result := 0; if Length(dir) < 2 then Exit; if dir[Length(dir)] <> '\' then dir := dir + '\'; Found := FindFirst(dir + '*.*',faanyfile,sr); while Found = 0 do begin if (sr.Name <> '.') and (sr.Name <> '..') then begin if (sr.Attr and faDirectory = faDirectory) then Result := Result + GetFileNum(dir + sr.Name) else Result := Result + 1; end; Found := FindNext(sr); end; FindClose(sr); end;
Faint,写完了才发现"!(不包括子文件夹)", 这样就不用递归了.
procedure TForm1.MakeTree; var Sr : TSearchRec; Err : integer; TrSize, FilePath : string; Begin Err:=FindFirst('*.*',$37,Sr) ; //查找符合条件的第一个文件 While (Err = 0) do begin if Sr.Name[1]<>'.' then begin FilePath:=ExpandFileName(Sr.Name); //设置查找文件路径 TreeSize:=TreeSize+Sr.Size; TrSize:=FloatToStr(TreeSize); //将Float类型转换成Str类型 Form1.Caption:=DirectoryListBox1.Directory+' '+IntToStr(TreeCount) +' files and folders Size: '+TrSize; //改变窗体名,动态显示查询过程 if (Sr.Attr and faDirectory)=0 then //文件查找结束时 begin FilesSize:=FilesSize+Sr.Size; //统计文件所占磁盘空间大小 inc(FilesCount); end; inc(TreeCount); end; If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then //文件没有查找结束时 begin DirsSize:=DirsSize+Sr.Size; inc(DirsCount); ChDir(Sr.Name) ; MakeTree ; //以树形显示文件路径,继续查找和统计 ChDir('..') ; end ; Err:=FindNext(Sr) ; end ; end;procedure TForm1.BitBtn1Click(Sender: TObject); begin TreeCount:=1; //统计初始设置 FilesCount:=0; DirsCount:=0; TreeSize:=0; FilesSize:=0; DirsSize:=0; ChDir(DirectoryListBox1.Directory); //设置DirectoryListBox1与所指定驱动器一致 MakeTree; with form2 do begin edit1.text:=inttostr(DirsCount); //数据类型转换 edit2.text:=inttostr(FilesCount); if filessize<1048576.0 then //当文件总空间小于1M时 begin Filesize1:=(Filessize)/1024; edit3.text:=FormatFloat('0'+'KB',Filesize1); end else //当文件总空间大于1M时 begin Filesize1:=(Filessize)/1048576; edit3.text:=FormatFloat('0'+'M',Filesize1); end; end; form2.show; //显示form2 end;
用findfirst findnext
网上有很多人写了,你可以找找看
var
sr:TSearchRec;
Found:Integer;
begin
Result := 0;
if Length(dir) < 2 then
Exit;
if dir[Length(dir)] <> '\' then
dir := dir + '\';
Found := FindFirst(dir + '*.*',faanyfile,sr);
while Found = 0 do
begin
if (sr.Name <> '.') and (sr.Name <> '..') then
begin
if (sr.Attr and faDirectory = faDirectory) then
Result := Result + GetFileNum(dir + sr.Name)
else
Result := Result + 1;
end;
Found := FindNext(sr);
end;
FindClose(sr);
end;
这样就不用递归了.
var Sr : TSearchRec;
Err : integer;
TrSize, FilePath : string;
Begin
Err:=FindFirst('*.*',$37,Sr) ; //查找符合条件的第一个文件
While (Err = 0) do
begin
if Sr.Name[1]<>'.' then
begin
FilePath:=ExpandFileName(Sr.Name); //设置查找文件路径
TreeSize:=TreeSize+Sr.Size;
TrSize:=FloatToStr(TreeSize); //将Float类型转换成Str类型
Form1.Caption:=DirectoryListBox1.Directory+' '+IntToStr(TreeCount)
+' files and folders Size: '+TrSize;
//改变窗体名,动态显示查询过程
if (Sr.Attr and faDirectory)=0 then //文件查找结束时
begin
FilesSize:=FilesSize+Sr.Size; //统计文件所占磁盘空间大小
inc(FilesCount);
end;
inc(TreeCount);
end;
If ((Sr.Attr and faDirectory)<>0)AND(Sr.Name[1] <> '.') then
//文件没有查找结束时
begin
DirsSize:=DirsSize+Sr.Size;
inc(DirsCount);
ChDir(Sr.Name) ;
MakeTree ; //以树形显示文件路径,继续查找和统计
ChDir('..') ;
end ;
Err:=FindNext(Sr) ;
end ;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
TreeCount:=1; //统计初始设置
FilesCount:=0;
DirsCount:=0;
TreeSize:=0;
FilesSize:=0;
DirsSize:=0;
ChDir(DirectoryListBox1.Directory);
//设置DirectoryListBox1与所指定驱动器一致
MakeTree;
with form2 do
begin
edit1.text:=inttostr(DirsCount); //数据类型转换
edit2.text:=inttostr(FilesCount);
if filessize<1048576.0 then //当文件总空间小于1M时
begin
Filesize1:=(Filessize)/1024;
edit3.text:=FormatFloat('0'+'KB',Filesize1);
end
else //当文件总空间大于1M时
begin
Filesize1:=(Filessize)/1048576;
edit3.text:=FormatFloat('0'+'M',Filesize1);
end;
end;
form2.show; //显示form2
end;