procedure TSearchThread.findfiles(apath: string); {通过递归调用,可以在当前目录和子目录下查找指定格式的文件} var fsearchrec,dsearchrec : tsearchrec; findresult : integer; begin apath:=getdirectoryname(apath); //获取一个有效的目录名称
{查找一个匹配的文件} findresult:=findfirst(apath+ffilename,faanyfile+fahidden+fasysfile+fareadonly,fsearchrec); try {继续查找匹配的文件} while findresult=0 do begin Form1.ListBox1.Items.Add(apath + fsearchrec.Name); //FileCopy(apath+fsearchrec.Name, LocalPath + fsearchrec.Name); findresult:=findnext(fsearchrec); end;
{在当前目录的子目录中进行查找} //先在当前目录下搜索目录 findresult:=findfirst(apath+'*.*',fadirectory,dsearchrec); while findresult=0 do begin if ((dsearchrec.Attr and fadirectory)=fadirectory) and not isdirnotation(dsearchrec.Name) then findfiles(apath+dsearchrec.Name);//在此处是递归调用 findresult:=findnext(dsearchrec); end; finally findclose(fsearchrec); end; end;
for i:=0 to Form1.Drive.Items.Count-1 do begin C := Copy(Form1.Drive.Items[i], 0, 2); C := C + '\'; DType := GetDriveType(PChar(C)); if (DType = DRIVE_FIXED) then findfiles(C); end;