我已经在Delphi中实现了这个代码,它将搜索文件或给出的名称,但它忽略了搜索所有子目录.如何才能做到这一点?
码:
if FindFirst(filePath,faAnyFile,searchResult)=0 then try repeat lbSearchResult.Items.Append(searchResult.Name); until FindNext(searchResult)<>0 except on e:Exception do ShowMessage(e.Message); end; //try ends FindClose(searchResult);
解决方法
如果您不需要线程,最简单的方法是:
procedure TForm1.AddAllFilesInDir(const Dir: string); var SR: TSearchRec; begin if FindFirst(IncludeTrailingBackslash(Dir) + '*.*',faAnyFile or faDirectory,SR) = 0 then try repeat if (SR.Attr and faDirectory) = 0 then ListBox1.Items.Add(SR.Name) else if (SR.Name <> '.') and (SR.Name <> '..') then AddAllFilesInDir(IncludeTrailingBackslash(Dir) + SR.Name); // recursive call! until FindNext(Sr) <> 0; finally FindClose(SR); end; end; procedure TForm1.Button1Click(Sender: TObject); begin ListBox1.Items.BeginUpdate; AddAllFilesInDir('C:\Users\Andreas Rejbrand\Documents\Aweb'); ListBox1.Items.EndUpdate; end;