如何得到指定目录下的所有文件名?(50分)

  • 主题发起人 主题发起人 vericky
  • 开始时间 开始时间
V

vericky

Unregistered / Unconfirmed
GUEST, unregistred user!
如题所示
 
findfirst findnext
 
转贴<br>轻轻松松查找文件<br>&nbsp; 在平常的编程当中,经常会碰到查找某一个目录下某一类文件或者所有文件的问题,为了适应不同的需要,我们经常不得不编写大量的类似的代码,有没有可能写一个通用的查找文件的程序,找到一个文件后就进行处理的呢?这样我们只要编写处理文件的部分就可以了,不需要编写查找文件的部分!答案是肯定的。下面的这个程序就能实现这个功能!<br>//说明:<br>//TFindCallBack为回调函数,FindFile函数找到一个匹配的文件之后就会调用这个函数。<br>//TFindCallBack的第一个参数找到的文件名,你在回调函数中可以根据文件名进行操作。<br>//TFindCallBack的第二个参数为找到的文件的记录信息,是一个TSearchRec结构。<br>//TFindCallBack的第三、四个参数分别为决定是否终止文件的查找,临时决定是否查找某个子目录!<br>//FindFile的参数:<br>//第一个决定是否退出查找,应该初始化为false;<br>//第二个为要查找路径;<br>//第三个为文件名,可以包含Windows所支持的任何通配符的格式;默认所有的文件<br>//第四个为回调函数,默认为空<br>//第五个决定是否查找子目录,默认为查找子目录<br>//第六个决定是否在查找文件的时候处理其他的消息,默认为处理其他的消息<br>//若有意见和建议请E_Mail:Kingron@163.net<br>type<br>&nbsp; TFindCallBack=procedure (const filename:string;const info:TSearchRec;var bQuit,bSub:boolean);<br><br><br>procedure FindFile(var quit:boolean;const path: String;const filename:string='*.*';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;proc:TFindCallBack=nil;bSub:boolean=true;const bMsg:boolean=true);<br>var<br>&nbsp; fpath: String;<br>&nbsp; info: TsearchRec;<br><br>&nbsp;procedure ProcessAFile;<br>&nbsp;begin<br>&nbsp; if (info.Name&lt;&gt;'.') and (info.Name&lt;&gt;'..') and ((info.Attr and faDirectory)&lt;&gt;faDirectory) then<br>&nbsp; begin<br>&nbsp; if assigned(proc) then<br>&nbsp; &nbsp; proc(fpath+info.FindData.cFileName,info,quit,bsub);<br>&nbsp; end;<br>&nbsp;end;<br><br>&nbsp;procedure ProcessADirectory;<br>&nbsp;begin<br>&nbsp; if (info.Name&lt;&gt;'.') and (info.Name&lt;&gt;'..') and ((info.attr and fadirectory)=fadirectory) then<br>&nbsp; &nbsp; findfile(quit,fpath+info.Name,filename,proc,bsub,bmsg);<br>&nbsp;end;<br><br>begin<br>if path[length(path)]&lt;&gt;'/' then<br>&nbsp; fpath:=path+'/'<br>else<br>&nbsp; fpath:=path;<br>try<br>&nbsp; if 0=findfirst(fpath+filename,faanyfile and (not fadirectory),info) then<br>&nbsp; begin<br>&nbsp; &nbsp; ProcessAFile;<br>&nbsp; &nbsp; while 0=findnext(info) do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ProcessAFile;<br>&nbsp; &nbsp; &nbsp; &nbsp; if bmsg then application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; &nbsp; if quit then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; findclose(info);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>finally<br>&nbsp; findclose(info);<br>end;<br>try<br>&nbsp; if bsub and (0=findfirst(fpath+'*',faanyfile,info)) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ProcessADirectory;<br>&nbsp; &nbsp; &nbsp; while findnext(info)=0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; ProcessADirectory;<br>&nbsp; &nbsp; end;<br>finally<br>&nbsp; findclose(info);<br>end;<br>end;<br>例子:<br>procedure aaa(const filename:string;const info:tsearchrec;var quit,bsub:boolean);<br>begin<br>&nbsp; form1.listbox1.Items.Add(filename);<br>&nbsp; quit:=form1.qqq;<br>&nbsp; bsub:=form1.checkbox1.Checked;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>listbox1.Clear;<br>qqq:=false;<br>button1.Enabled:=false;<br>findfile(qqq,edit1.text,edit2.text,aaa,checkbox1.checked,checkbox2.checked);<br>showmessage(inttostr(listbox1.items.count));<br>button1.Enabled:=true;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>qqq:=true;<br>end;<br>
 
procedure TForm1.Button1Click(Sender: TObject);<br><br>var<br>&nbsp; sr: TSearchRec;<br>&nbsp; FileAttrs: Integer;<br>begin<br>&nbsp; StringGrid1.RowCount := 1;<br>&nbsp; if CheckBox1.Checked then<br>&nbsp; &nbsp; FileAttrs := faReadOnly<br>&nbsp; else<br>&nbsp; &nbsp; FileAttrs := 0;<br>&nbsp; if CheckBox2.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faHidden;<br>&nbsp; if CheckBox3.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faSysFile;<br>&nbsp; if CheckBox4.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faVolumeID;<br>&nbsp; if CheckBox5.Checked then<br><br>&nbsp; &nbsp; FileAttrs := FileAttrs + faDirectory;<br>&nbsp; if CheckBox6.Checked then<br>&nbsp; &nbsp; FileAttrs := FileAttrs + faArchive;<br>&nbsp; if CheckBox7.Checked then<br><br>&nbsp; &nbsp; FileAttrs := FileAttrs + faAnyFile;<br><br>&nbsp; with StringGrid1 do<br>&nbsp; begin<br>&nbsp; &nbsp; RowCount := 0;<br><br>&nbsp; &nbsp; if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then<br><br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; &nbsp; if (sr.Attr and FileAttrs) = sr.Attr then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; RowCount := RowCount + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp; Cells[1,RowCount-1] := sr.Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; Cells[2,RowCount-1] := IntToStr(sr.Size);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; until FindNext(sr) &lt;&gt; 0;<br>&nbsp; &nbsp; &nbsp; FindClose(sr);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;
 
procedure findall(disk,path: String; var fileresult: Tstrings); <br>var <br>fpath: String; <br>fs: TsearchRec; <br>begin <br>fpath:=disk+path+'/*.*'; <br>if findfirst(fpath,faAnyFile,fs)=0 then <br>begin <br>if (fs.Name&lt;&gt;'.')and(fs.Name&lt;&gt;'..') then <br>if (fs.Attr and faDirectory)=faDirectory then <br>findall(disk,path+'/'+fs.Name,fileresult) <br>else <br>fileresult.add(disk+strpas(strupper(pchar(path)))+'/'+strpas( <br>strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')'); <br>while findnext(fs)=0 do <br>begin <br>if (fs.Name&lt;&gt;'.')and(fs.Name&lt;&gt;'..') then <br>if (fs.Attr and faDirectory)=faDirectory then <br>findall(disk,path+'/'+fs.Name,fileresult) <br>else <br>fileresult.add(disk+strpas(strupper(pchar(path)))+'/'+str <br>pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')'); <br>end; <br>end; <br>findclose(fs); <br>end; <br><br>读取返回值fileresult.items即可
 
用API的话就同上面那些, 不过我有的时候是用一个文件列表框得到的, 比较省代码, 呵呵。
 
最简单的办法用filelistbox,你只要修改他的Directory属性为你的目录,然后<br>就可以用FileListBox1.Items.Strings可以得到所有的文件名。
 
多人接受答案了。
 
后退
顶部