望提供例子

解决方案 »

  1.   

    FindFirst/FindNext/FindCloseDelphi Help 有Example
      

  2.   

    跟着楼上的补充提示一下: 碰上目录时需要递归,所以你需要编写递归函数。不是例子,没有办法有例子了,直接帮你做作业了。procedure SearchFile( const dir, pattern: string );
    var
     sr: TSearchRec;
    begin
     if FindFirst(dir + '\*', faAnyFile and not faVolumeID, sr)=0 then
     begin
      repeat
       if (sr.Attr and faDirectory)=0 then
       begin
        if MatchesMask(sr.Name, pattern) then
         writeln( dir+'\'+sr.Name );
       end
       else if (sr.Name<>'.') and (sr.Name<>'..') then
       begin
        SearchFile( dir + '\' + sr.Name, pattern );
       end;  until FindNext(sr)<>0;
      FindClose( sr );
     end;
    end;SearchFile( 'D:', '*.rm' );
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        ListBox1: TListBox;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        procedure Findds(AStrings: TStrings; APath, Sourfile: string);
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);//方法一
    var
      s:String;
    begin
      s:='d:\temp\*.rm';
      SendMessage(ListBox1.Handle,LB_DIR,DDL_ARCHIVE or DDL_HIDDEN or DDL_SYSTEM or DDL_READWRITE,Integer(s));end;procedure TForm1.Button2Click(Sender: TObject);
    begin  Findds(Memo1.Lines,'d:\a\','*.rm');
    end;procedure TForm1.Findds(AStrings: TStrings; APath, Sourfile: string);//方法二
    var
      FSearchRec : TSearchRec;
      FindResult : integer;
      TmpList:TStringList;
      i : integer;
        function IsDirNot(A : string) : boolean;
      begin
        Result := (a = '.') or (a = '..');
      end;
    begin
      try
       TmpList:=TStringList.Create;
       TmpList.Clear;
       FindResult := FindFirst(Apath+Sourfile,faDirectory,FSearchRec);
       while FindResult = 0 do
       begin
          if ((FSearchRec.Attr and fadirectory) = faDirectory) then
          begin  
            if  not IsDirNot(FSearchRec.Name) then begin
            tmplist.Add(apath+FSearchRec.Name);        Findds(AStrings, APath + FSearchRec.Name + '\',Sourfile);
            end;
          end else tmplist.Add(apath+FSearchRec.Name);
          FindResult := FindNext(FSearchRec);
       end;
          for i := 0 to TmpList.Count -1 do
         AStrings.Add(TmpList.Strings[i]);
       TmpList.Free;
      finally
       FindClose(FSearchRec);
      end;
    end;
    end.
      

  4.   

    FT, 反对楼主,我的代码虽然短些,但却是功能最强的,结果我的分比wudi_1982的少。我说说wudi_1982代码的问题:
    1、使用了传入的文件名(通配符,如'*.rm')作为搜索条件,这样把目录也过滤了,事实上,只能检索到与SourceFilre ('*.rm')匹配的目录名。这是不符常规要求的;
    2、我的代码还能找到隐藏的、系统的文件和目录,而wudi_1982的不行;
    3、wudi_1982的代码还返回了与SourceFilre ('*.rm')匹配的目录名,这没有什么意义吧?
    4、wudi_1982使用TmpList没什么意义,只是浪费空间和时间。其实可以直接使用AStrings;