如何取得某个文件夹下的文件数目????(50分)

  • 主题发起人 torrentwang
  • 开始时间
T

torrentwang

Unregistered / Unconfirmed
GUEST, unregistred user!
如何取得某个文件夹下的文件数目????
 
var FileList:TFileListBox;
FileList := TFileListBox.Create(Self);
FileList.Parent := Self;
FileList.Visible := False;
FileList.Mask := '*.*';
FileList.Directory := Path;
FileList.Items.Count //文件数目,
很差的办法
 
用findfirst、findnext和findclose可以获取到详细的信息。
 
demo中有例子
 
我想要的是目录下的所有文件,包括子目录中的文件,数目总和
 
procedure findall(disk,path: String; var fileresult: Tstrings);
var
fpath: String;
fs: TsearchRec;
begin
fpath:=disk+path+'/*.*';
if findfirst(fpath,faAnyFile,fs)=0 then
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(disk,path+'/'+fs.Name,fileresult)
else
fileresult.add(disk+strpas(strupper(pchar(path)))+'/'+strpas(
strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
while findnext(fs)=0 do
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(disk,path+'/'+fs.Name,fileresult)
else
fileresult.add(disk+strpas(strupper(pchar(path)))+'/'+str
pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
end;
end;
findclose(fs);
end;


fileresult.count即为所求
 
谁知道如何调用这个程序,我怎么发现老是错误

aa:Tstrings;
begin
aa:=Tstrings.Create;
findall('',MyPath,aa);
...

 
如何调用procedure findall(disk,path: String; var fileresult: Tstrings);

关键是Tstrings的用法问题
 
改成:
procedure findall(disk,path: String; var fileresult: Tstringlist);

调用:
procedure TForm1.Button1Click(Sender: TObject);
var li:TstringList;
begin
li:=TstringList.create;
try
findall('',edit1.text,li);
label1.caption:=inttostr(li.count);
finally
li.free;
end;
end;
 
接受答案了.
 
顶部