如题

解决方案 »

  1.   

    unit U_Main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, StrUtils, ExtCtrls;type
      TfrmMain = class(TForm)
        pnlClient: TPanel;
        memSearchResult: TMemo;
        pnlTop: TPanel;
        lblSearchPath: TLabel;
        lblSearchFile: TLabel;
        lblSearchResult: TLabel;
        edtSearchPath: TEdit;
        btnStartSearch: TButton;
        edtSearchFile: TEdit;
        procedure btnStartSearchClick(Sender: TObject);
      private
        { Private declarations }
        procedure GetFilenames(sPath, sFilename: String;  AList: TStrings);
        procedure Search(sPath, sFilename: String;  AList: TStrings);
      public
        { Public declarations }
      end;var
      frmMain: TfrmMain;implementation{$R *.dfm}procedure TfrmMain.GetFilenames(sPath, sFilename: String;  AList: TStrings);
    //功能描述: 列出sPath目录中(不含子目录)所有文件名符合sFilename规则的文件名
    //入口参数:
    //  sPath     - 目录路径
    //  sFilename - 文件名
    //出口参数:
    //  AList     - sPath目录中所有符合的文件名被添加到了这一列表中
    var
      SR : TSearchRec;
    begin
      if FindFirst(sPath + sFilename, faReadOnly + faHidden + faSysFile + faArchive + faAnyFile, SR) = 0 then
      begin
        repeat
          AList.Add(sPath + SR.Name);
        until FindNext(SR) <> 0;
        FindClose(SR);
      end;
    end;procedure TfrmMain.Search(sPath, sFilename: String;  AList: TStrings);
    //功能描述: 遍历sPath目录及其子目录中所有的文件名符合sFilename规则的文件名
    //入口参数:
    //  sPath     - 目录路径
    //  sFilename - 文件名
    //出口参数:
    //  AList     - 所有符合的文件名被添加到了这一列表中
    var
      SR: TSearchRec;
    begin
      GetFilenames(sPath, sFilename, AList);
      if FindFirst(sPath + '*.*' , faReadOnly + faHidden + faArchive + faDirectory, SR) = 0 then
      begin
        repeat
          if (SR.Attr and faDirectory) = faDirectory then
          begin
            if (SR.Name <> '.') and (SR.Name <> '..') then
            begin
              Search(sPath + sR.Name + '\', sFilename, AList);
            end;
          end;
        until FindNext(SR) <> 0;
        FindClose(SR);
      end;
    end;procedure TfrmMain.btnStartSearchClick(Sender: TObject);
    //事件描述: 开始查找
    var
      sPath : String;
    begin
      memSearchResult.Lines.Clear;
      sPath := edtSearchPath.Text;
      //保证sPath的最后一个字符为'\'
      if RightStr(sPath, 1) <> '\' then sPath := sPath + '\';
      Search (sPath, edtSearchFile.Text, memSearchResult.Lines);
    end;end.