我自创的查找算法,不用递归,速度快,可显示进度
{
参数:
DirName 查找的目录
FileList 找到的文件, 需要调用前创建
IncSubDir 是否包括子目录,True包括,否则不包括。默认包括子目录。
}
procedure FindFile(DirName:string;var FileList:TStrings
IncSubDir:boolean=True);
var DS:TSearchRec;
i,p:integer;
Path:string;
DirList:TStrings;
begin
i := 0;
DirName :=ExcludeTrailingPathDelimiter(DirName);
DirList := TStringList.Create;
DirList.Add(DirName);
while i < DirList.Count do
begin
Path := DirList.Strings + '/';
if FindFirst(Path + '*.*', faAnyFile, Ds) = 0 then
repeat
if (Ds.Attr and faDirectory) = 0 then
FileList.Add(Path + Ds.Name)
else
if IncSubDir and (Ds.Name[1] <> '.') then
DirList.Add(Path + Ds.Name);
until FindNext(Ds) <> 0;
Inc(i);
//显示进度
p := i * 100 div DirList.Count;
if p > Form1.ProgressBar1.Position then
Form1.ProgressBar1.Position := p;
FindClose(Ds);
end;
DirList.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var FileList:TStrings;
begin
FileList := TStringList.Create;
FindFile('c:', FileList)
//查找C盘所有文件
FileList.Free;
end;