如何取得某个目录下的所有文件名。?(100分)

  • 主题发起人 主题发起人 hugd1001
  • 开始时间 开始时间
H

hugd1001

Unregistered / Unconfirmed
GUEST, unregistred user!
如何取得某个目录下的所有文件名。?
 
findfirst()<br>findnext()<br>findclose()<br>这三个api
 
var<br>&nbsp; f: TSearchRec;<br>&nbsp; Ret: Integer;<br>&nbsp; FileName: string;<br>begin<br>&nbsp; Ret:=FindFirst('DirName'+'/*.*', faAnyFile, f);<br>&nbsp; while Ret&lt;&gt;0 do<br>&nbsp; begin<br>&nbsp; &nbsp; FileName:=f.Name;<br>&nbsp; &nbsp; //Do something with FileName<br>&nbsp; &nbsp; Ret:=FindNext(f)<br>&nbsp; end;<br>&nbsp; FindClose(f)<br>end;
 
同意 LeeChange
 
procedure TForm1.FindAll(const Path: String);<br>var<br>&nbsp; sr:TSearchRec;<br>&nbsp; fr:Integer;<br>begin<br>fr:=FindFirst(Path,faAnyFile,sr);<br>while fr=0 do<br>&nbsp; begin<br>&nbsp; if (sr.Attr=faDirectory)and(sr.Name&lt;&gt;'.')and(sr.Name&lt;&gt;'..') then<br>&nbsp; &nbsp; FindAll(sr.Name) &nbsp;//递归查找下一个目录<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; //处理文件<br>&nbsp; &nbsp; end;<br>&nbsp; FindNext(sr);<br>&nbsp; end;<br>FindClose(sr);<br>end;<br><br><br>其实还有一个变通的方法,用一个FileListBox控件,将其visible设为false,并在代码<br>中设置其Directory属性为指定目录,然后遍历它的items即可找出所有文件名。<br>
 
memo1.Lines.SaveToFile('c:/aaa.txt');<br>winexec('cmd.exe /c dir c:/ &gt;&gt;c:/aaa.txt',sw_hide);<br>sleep(1000);//等一下,否则aaa.txt还没有东西呢。<br>memo1.Lines.LoadFromFile('c:/aaa.txt');<br><br>如果你愿意用这样也行。<br><br>
 
下面这段代码将C:/windows下的所有文件的路径添加到ListBox1,文件名添加到ListBox2<br>我想你只是需要将Listbox2更换为TStringList变量就行了。<br>procedure TForm1.searchpath(path: string);<br>var<br>&nbsp; searchrec: TSearchRec;<br>begin<br>&nbsp; if path[length(path)] &lt;&gt; '/' then<br>&nbsp; &nbsp; path := path + '/';<br>&nbsp; if FindFirst(path + '*.*', faAnyFile, SearchRec) &lt;&gt; 0 then exit;<br>&nbsp; if (searchrec.Attr and faDirectory) &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; if (searchrec.name &lt;&gt; '.') and (searchrec.name &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp; searchpath(path + searchrec.name);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; ListBox1.Items.Add(path + searchrec.name);<br>&nbsp; &nbsp; lIstBox2.Items.Add(searchrec.name);<br>&nbsp; end;<br>&nbsp; while findnext(searchrec) = 0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if (searchrec.Attr and faDirectory) &lt;&gt; 0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (searchrec.name &lt;&gt; '.') and (searchrec.name &lt;&gt; '..') then<br>&nbsp; &nbsp; &nbsp; &nbsp; searchpath(path + searchrec.name);<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add(path + searchrec.name);<br>&nbsp; &nbsp; &nbsp; lIstBox2.Items.Add(searchrec.name);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; findclose(searchrec);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; searchpath('C:/windows');<br>end;<br>
 
拖一个button,一个ListBox到Form上<br><br>procedure TForm1.ListFilesName(strlist: TstringList; path: string);<br>var<br>&nbsp; searchrec: TSearchRec;<br>&nbsp; Found &nbsp; &nbsp;: Integer;<br>begin<br>&nbsp; if path[length(path)] &lt;&gt; '/' then<br>&nbsp; &nbsp; path := path + '/';<br><br>&nbsp; Found := FindFirst(path+'*.*',fadirectory,searchRec);<br>&nbsp; while Found = 0 do<br>&nbsp; begin<br>&nbsp; &nbsp; if (Searchrec.Attr and fadirectory &lt;&gt; 0) and (Searchrec.Name[1] &lt;&gt; '.') then<br>&nbsp; &nbsp; &nbsp; ListFilesName(strList,Path+SearchRec.Name)<br>&nbsp; &nbsp; else if (searchrec.Name[1] &lt;&gt; '.') then<br>&nbsp; &nbsp; &nbsp; strlist.Add(Searchrec.Name);<br>&nbsp; &nbsp; Found := FindNext(SearchRec);<br>&nbsp; end;<br><br>&nbsp; FindClose(SearchRec);<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; list : TStringList;<br>begin<br>&nbsp; List := TStringlist.Create;<br>&nbsp; ListFilesName(List,'D:/Parser/Parser');//自己的一个目录<br>&nbsp; Listbox1.Items.Assign(List);<br>&nbsp; List.Free;<br>end;<br>
 
procedure TForm1.Button2Click(Sender: TObject);<br>var<br>rec:TSearchRec;<br>begin<br>if SysUtils.FindFirst('e:/mp3/*.*',faanyfile,rec)=0 then<br>&nbsp; try<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; memo1.lines.add('e:/mp3/'+rec.Name));<br>&nbsp; &nbsp; until sysutils.FindNext(rec)&lt;&gt;0;<br>&nbsp; &nbsp;finally<br>&nbsp; &nbsp; sysutils.FindClose(Rec);<br>&nbsp; end;<br>end;
 
多人接受答案了。
 
后退
顶部