怎样获得某一个目录下的某一种类型的文件?(包括子目录的文件)(100分)

  • 主题发起人 主题发起人 ranivy
  • 开始时间 开始时间
R

ranivy

Unregistered / Unconfirmed
GUEST, unregistred user!
例如我想找出 C:/AB目录下(包括子目录)的所有 &nbsp;'.exe' 文件该怎么做?<br>
 
查findfirst的help就有例子
 
用FindFirst和FindNext搜索 'C:/AB/*.EXE'就可以了
 
用fileFirst和FileNext一个个的搜索,然后用<br>ExtractFileExt(filelistbox1.filename);//取文件后缀<br>加以判断文件是什么类型!
 
前面几位老兄都对,这里给出完整代码<br>var &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; sr:TSearchRec; <br>。。。<br><br>if (FindFirst('c:/ab/''*.exe,faAnyFile and not faDirectory,sr)=0) then &nbsp;//是文件不是目录<br>&nbsp; try<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; ...//处理,sr.name用于取出文件名<br>&nbsp; &nbsp; until (FindNext(sr) &lt;&gt; 0);<br>&nbsp; finally<br>&nbsp; &nbsp; FindClose(sr);<br>&nbsp; end;<br>可以运行。
 
上述的方法都不能查找到子目录一级<br>function TForm1.searchDir(sDirName:String):boolean;<br>var<br>&nbsp; &nbsp;hFindFile:Cardinal;<br>&nbsp; &nbsp;tfile:String;<br>&nbsp; &nbsp;FindFileData:WIN32_FIND_DATA;<br>begin<br>&nbsp; &nbsp;hFindFile:=FindFirstFile('*.exe',FindFileData);<br>&nbsp; &nbsp;if hFindFile&lt;&gt;INVALID_HANDLE_VALUE then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tfile:=FindFileData.cFileName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tfile='.') or (tfile='..') then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Continue;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if sDirName[Length(sDirName)]&lt;&gt;'/' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; searchDir(sDirName+'/'+tfile)<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; searchDir(sDirName+tfile);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;StringList.add(sDirName+'/'+tfile);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; until FindNextFile(hFindFile,FindFileData)=false;<br>// &nbsp; &nbsp; &nbsp; &nbsp;FreeAndNil(hFindFile);<br>&nbsp; &nbsp;end<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; result:=false;<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp;end;<br>&nbsp; &nbsp;//回到原来的目录下<br>&nbsp; &nbsp;result:=true;<br>end;<br>
 
naught 兄,其实这个问题我在论坛已经找到答案,和你的答案是一样的,但问题是我每次运行<br>都产生错误,不知道什么原因,我就是在你的代码下加了一些代码:<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>searchDir('c:/ab');<br>end;<br>还有一个问题,那个STRINGLIST该是TSTRINGS类型吧?你那里没写
 
先要 StringList := TStringList.Create;
 
to:不好意思,你的答案不够完整,所以不能给满分你<br>NAUGHTBOY之前的几位朋友,下次请你们回答问题前先看清楚题目
 
后退
顶部