一段不知道从那抄来的函数,看看对你有没有帮助<br>/////////////////////////////////////////////////////////////////<br>//<br>// 查询主体函数<br>// Mainpath: 指定的查询目录。<br>// Filename: 欲查询的文件。<br>// Foundresult: 返回的含完整路径的匹配文件(可能有多个)。<br>// 如果有匹配文件,函数返回True,否则,返回False;<br>/////////////////////////////////////////////////////////////////<br>function SearchFile(mainpath:string;filename:string;var foundresult:TStringList):Boolean;<br>var<br> i:integer;<br> Found:Boolean;<br> subdir1:TStrings;<br> searchRec:TsearchRec;<br>begin<br> found:=false;<br> if Trim(filename)<>'' then<br> begin<br> subdir1:=TStringList.Create;//字符串列表必须动态生成<br> //找出所有下级子目录。<br> if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then<br> begin<br> if IsValidDir(SearchRec) then<br> subdir1.Add(SearchRec.Name);<br> while (FindNext(SearchRec) = 0) do<br> begin<br> if IsValidDir(SearchRec) then<br> subdir1.Add(SearchRec.Name);<br> end;<br> end;<br> FindClose(SearchRec);<br> //查找当前目录。<br> if FileExists(mainpath+filename) then<br> begin<br> if (FindFirst(mainpath+filename, faAnyFile, SearchRec)=0) then<br> begin<br> while (FindNext(SearchRec) = 0) do<br> begin<br> foundresult.Add(mainpath+SearchRec.Name);<br> found:=true;<br> end;<br> FindClose(SearchRec);<br> end;<br> end;<br> //这是递归部分,查找各子目录。<br> for i:=0 to subdir1.Count-1 do<br> found:=Searchfile(mainpath+subdir1.Strings+'/',Filename,foundresult)or found;<br> //资源释放并返回结果。<br> subdir1.Free;<br> end;<br> result:=found;<br>end;