Chinaunix首页 | 论坛 | 博客
  • 博客访问: 230487
  • 博文数量: 73
  • 博客积分: 3005
  • 博客等级: 中校
  • 技术积分: 857
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-24 10:11
文章分类

全部博文(73)

文章存档

2014年(2)

2011年(5)

2010年(29)

2009年(32)

2008年(5)

我的朋友

分类:

2009-11-17 14:43:47

FindFirst  是用来寻找目标目录下的第一个文件,
FindFirst函数在delphi帮助下的定义:
function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer;
其中有一句:FindFirst returns 0 if a file was successfully located
也就是说,当成功找到文件时,返回0.

FindNext   则是寻找下一个
TSearchRec 是一个文件信息的纪录,当FindFirst返回SearchRec时,你可以通过SearchRec.Name获取文件名,以及SearchRec.Size获取文件大小等信息。

unit   unit1;  
interface  
uses  
      Windows,   Messages,   SysUtils,   Classes,   Graphics,   Controls,   Forms,   Dialogs,  
      Db,   DBTables,   StdCtrls,   DBCtrls,   Mask,   ExtCtrls;  
type  
      TForm1   =   class(TForm)  
          Button1:   TButton;  
          Memo1:   TMemo;  
          Edit1:   TEdit;  
          procedure   Button1Click(Sender:   TObject);  
      private  
          {   Private   declarations   }  
      public  
          {   Public   declarations   }  
      end;  
   
var  
      Form1:   TForm1;  
      countd,countf:integer;  
implementation  
   
{$R   *.DFM}  
   
procedure   search(dir:string);  
var  
      targetpath:string;{目标路径名}  
      sr:TsearchRec;  
begin  
     
     {第一阶段:找出初始dir目录下的所有文件,其中dir变量值由edit1的Text属性确定}  
      targetpath:=extractfilepath(dir); {分解出目标路径名}  
      if   findfirst(dir,faanyfile,sr)=0   then  
      repeat  
        if((sr.name<>'.')and(sr.name<>'..')  {排除父目录和本目录两个假文件}  
        and((filegetattr(targetpath+sr.name)and   fadirectory)<>fadirectory)) {只取文件}  
        then  
            begin  
            form1.memo1.Lines.Add(targetpath+sr.name);{在memo中添加找到的文件}  
            countf:=countf+1;  
            end  
      until   findnext(sr)<>0;  
   
      if   findfirst(dir,faanyfile,sr)=0   then  
      repeat  
        if((sr.name<>'.')and(sr.name<>'..')){排除父目录和本目录两个假文件}  
        and((filegetattr(targetpath+sr.name)and   fadirectory)=fadirectory){排除文件}  
        then  
          begin  
            search(targetpath+sr.name+'\*.*');{递归调用}  
            form1.memo1.Lines.Add(targetpath+sr.name);  
   
            countd:=countd+1;  
          end  
      until   findnext(sr)<>0;  
   
end;  
   
procedure   TForm1.Button1Click(Sender:   TObject);  
begin  
      countf:=0;  
      countd:=0;  
      memo1.Clear;{清除数据表memo字段内容}  
      search(Edit1.Text);{调用Search()函数}  
      MessageDlg('文件搜索完毕!',mtInformation,[mbOk],0);{结束提示}  
      showmessage('目录:'+inttostr(countd)+'   文件:'+inttostr(countf))  
end;  
   
end.
阅读(2371) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~