代码如下,你自己修改一下吧,'*.*'改为其他类型就可以了<br>1. 从搜索记录中判断是否是子目录。<br>function IsValidDir(SearchRec:TSearchRec):Boolean;<br>begin<br>if (SearchRec.Attr=16) and<br>(SearchRec.Name<>'.') and<br>(SearchRec.Name<>'..') then<br>Result:=True<br>else<br>Result:=False;<br>end;<br>2. 这是查询主体函数。<br>参数介绍:<br>Mainpath: 指定的查询目录。<br>Filename: 欲查询的文件。<br>Foundresult: 返回的含完整路径的匹配文件(可能有多个)。<br>如果有匹配文件,函数返回True,否则,返回False;<br>function SearchFile(mainpath:string;filename:string;<br>var foundresult:TStrings):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>found:=true;<br>foundresult.Add(mainpath+filename);<br>end;<br>//这是递归部分,查找各子目录。<br>for i:=0 to subdir1.Count-1 do<br>found:=Searchfile(mainpath+subdir1.Strings+<br>'/',Filename,foundresult)or found;<br>//资源释放并返回结果。<br>subdir1.Free;<br>end;<br>result:=found;<br>end;<br>