关于文件查找的问题 (菜鸟级)在线等待(100分)

  • 主题发起人 wangyunfei_tony
  • 开始时间
W

wangyunfei_tony

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样才可以得到某目录下的特定类型文件的文件名列表。<br>--好像有点绕口,不知说明白没有,不胜感激[:)]
 
简单的例子: 搜索当前目录下的pas文件并显示文件名到memo1中<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; Finddata: TWin32FindData;<br>&nbsp; h: Integer;<br>&nbsp; ws: WideString;<br>&nbsp; s: string;<br>begin<br>&nbsp; h := findfirstfile('*.pas', finddata);<br>&nbsp; if h &lt;&gt; INVALID_HANDLE_VALUE then<br>&nbsp; begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; if finddata.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY=0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ws := finddata.cFileName;<br>&nbsp; &nbsp; &nbsp; &nbsp; s := ws;<br>&nbsp; &nbsp; &nbsp; &nbsp; memo1.lines.add(s);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; until not findnextfile(h, finddata);<br>&nbsp; &nbsp; windows.findclose(h);<br>&nbsp; end;<br>end;<br><br>
 
[?]当前目录?其他目录怎么办?
 
代码如下,你自己修改一下吧,'*.*'改为其他类型就可以了<br>1. 从搜索记录中判断是否是子目录。<br>function IsValidDir(SearchRec:TSearchRec):Boolean;<br>begin<br>if (SearchRec.Attr=16) and<br>(SearchRec.Name&lt;&gt;'.') and<br>(SearchRec.Name&lt;&gt;'..') 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)&lt;&gt;'' 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>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
顶部