找出指定目录下的所有以.abc结尾的文件
findSch(path:sting;Fname:string):Tstrings;不知明白否
在线等~~

解决方案 »

  1.   

    dir *.abc>a.txt;呵呵,玩笑;
    FINDFIRST;
      

  2.   

    对 例如查找当前目录下的所有 *.txt文件
      

  3.   

    procedure SearchFileEx(const Dir, Ext: string; Files: TStrings);
    var
      Found: TSearchRec;
      i: integer;
      Dirs: TStrings;
      Finished: integer;
      StopSearch: Boolean;
    begin
      StopSearch := False;
      Dirs := TStringList.Create;
      Finished := FindFirst(Dir + '*.*', 63, Found);
      while (Finished = 0) and not (StopSearch) do
      begin
        if (Found.Name <> '.') then
        begin
          if (Found.Attr and faDirectory) = faDirectory then
            Dirs.Add(Dir + Found.Name)
          else
            if Pos(UpperCase(Ext), UpperCase(Found.Name)) > 0 then
              Files.Add(Dir + Found.Name);
        end;
        Finished := FindNext(Found);
      end;
      FindClose(Found);
      if not StopSearch then
        for i := 0 to Dirs.Count - 1 do
          SearchFileEx(Dirs[i], Ext, Files);
      Dirs.Free;
    end;Files中所存既是
      

  4.   

    以下的程序是将bak为扩展名的文件加入listview里。指定应程序路径为当前路径。
    procedure Tbackfrm.refreshlistview;
    var
      sr: TSearchRec;
      filename: TStrings;
      dirName: string;
      i:integer;
    begin
      dirName := Extractfilepath(application.exeName)+'back\';// 应用程序back文件夹下
      filename:= TStringList.Create;
      if FindFirst(dirName + '*.bak',faAnyFile,sr) = 0 then//所有以bak为扩展名,你可以指定一下。
      begin
        repeat
          if (sr.Attr and faAnyFile) = sr.Attr then
              filename.Add(sr.Name);    until FindNext(sr) <> 0;
        FindClose(sr);
      for  i:=0  to   filename.Count-1 do
      begin
        listview1.Items.Add;
        listview1.Items.Item[i].Caption:=filename[i]
      end;
        filename.Free;
      end;
    end;
      

  5.   

    procedure TSearchThread.ScanForStr(const FName: string;
      var FileStr: string);
    { 此过程用于从一个文件中扫描字符串}
    var
      Marker: string[1];
      FoundOnce: Boolean;
      FindPos: integer;
    begin
      FindPos := Pos(SearchStr, FileStr);
      FoundOnce := False;
      while (FindPos <> 0) and not Terminated do
      begin
        if not FoundOnce then
        begin
          if FileNames then
            Marker := ''
          else
            Marker := ':';
          { 添加文件到列表框}
          AddStr := Format('File %s%s', [FName, Marker]);
          Synchronize(AddToList);
          FoundOnce := True;
        end;
        if FileNames then Exit;    AddStr := GetCurLine(FileStr, FindPos);
        Synchronize(AddToList);
        FileStr := Copy(FileStr, FindPos + Length(SearchStr),
          Length(FileStr));
        FindPos := Pos(SearchStr, FileStr);
      end;
    end;procedure TSearchThread.SearchFile(const FName: string);
    { 此过程用于搜索文件名称 }
    var
      DataFile: THandle;
      FileSize: Integer;
      SearchString: string;
    begin
      FSearchFile := FName;
      Synchronize(SetSearchFile);
      try
        DataFile := FileOpen(FName, fmOpenRead or fmShareDenyWrite);
        if DataFile = 0 then raise Exception.Create('');
        try
          FileSize := GetFileSize(DataFile, nil);
          SetLength(SearchString, FileSize);
          FileRead(DataFile, Pointer(SearchString)^, FileSize);
        finally
          CloseHandle(DataFile);
        end;
        if not CaseSens then SearchString := UpperCase(SearchString);
        ScanForStr(FName, SearchString);
      except
        on Exception do
        begin
          AddStr := Format('Error reading file: %s', [FName]);
          Synchronize(AddToList);
        end;
      end;
    end;procedure TSearchThread.FindAllFiles(const Path: string);
    { 查找所有匹配的文件}
    var
      SR: TSearchRec;
    begin
      { 查找第一个匹配的文件}
      if FindFirst(Path + FileSpec, faArchive, SR) = 0 then
        try
          repeat
            SearchFile(Path + SR.Name);
          until (FindNext(SR) <> 0) or Terminated; // 寻找下一个文件
        finally
          SysUtils.FindClose(SR);                   // 最后清除查找记录
        end;
    end;procedure TSearchThread.DoSearch(const Path: string);
    {该过程用于在子目录树中递归查找}
    var
      SR: TSearchRec;
    begin
      { 查找目录 }
      if FindFirst(Path + '*.*', faDirectory, SR) = 0 then
        try
          repeat
            { 如果是一个目录... }
            if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') and
              not Terminated then
            begin
              FindAllFiles(Path + SR.Name + '\');  // 在目录中查找
              DoSearch(Path + SR.Name + '\');      // 递归查找
            end;
          until (FindNext(SR) <> 0) or Terminated;      // 寻找下一个目录
        finally
          SysUtils.FindClose(SR);                       // 最后清除查找记录
        end;
    end;
      

  6.   

    if FindFirst(ExtractFileName(Application.ExeName) + '\*.exe', faArchive, SrchRec) = 0 then
      showmessage('Found a exe file')
      

  7.   

    to  dyxfkj(我爱我老婆) 
    程序无法用,编译错误
    能不能转成函数啊
      

  8.   

    框架写好,看你的需求,再写FindCall函数
    type
      TForm1 = class(TForm)
        Button1: TButton;
        CheckBox1: TCheckBox;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
      end;var
      Form1: TForm1;implementation
    {$R *.dfm}type
      TCallBackFind = function(const Path, FileName: string; Param: Pointer): Boolean; stdcall;
      PFindFile = ^TFindFile;
      TFindFile = record
        Path: string;
        Param: Pointer;
        Callback: TCallBackFind;
      end;function FindFileThread(Find: PFindFile): DWORD; stdcall;  function DoCallback(const Path, FileName: string): Boolean;
      begin
        Result := False;
        if Assigned(Find^.Callback) then
          Result := Find^.Callback(Path, FileName, Find^.Param);
      end;  function SearchPath(const Path: string): Boolean;
      var
        Rec: TSearchRec;
      begin
        Result := True;
        if FindFirst(Path + '\*.*', faAnyFile, Rec) = 0 then
        try
          repeat
            if (Rec.Name <> '.') and (Rec.Name <> '..') then
            begin
              if (Rec.Attr and faDirectory) = faDirectory then
                Result := SearchPath(Path + '\' + Rec.Name)
              else
                Result := DoCallback(Path, Rec.Name);
              if not Result then break;
            end;
          until FindNext(Rec) <> 0;
        finally
          FindClose(Rec);
        end;
      end;begin
      Result := 0;
      if not Assigned(Find) then Exit;
      try
        SearchPath(Find^.Path);
      finally
        FreeMem(Find);
      end;
    end;procedure FindAllFile(const Path: string; Callback: TCallbackFind; Param: Pointer);
    var
      Find: PFindFile;
      ThreadID: Cardinal;
    begin
      New(Find);
      Find^.Path := Path;
      Find^.Param := Param;
      Find^.Callback := Callback;
      CloseHandle(CreateThread(nil, 0, @FindFileThread, Find, 0, ThreadID));
    end;function FindCall(const Path, FileName: string; Form: TForm1): Boolean; stdcall;
    begin
      Result := Form.CheckBox1.Checked;
      if Result then
        Form.Memo1.Lines.Add(Format('Path: %s, FileName: %s', [Path, FileName]));
      //Application.ProcessMessages;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      CheckBox1.Checked := True;
      FindAllFile('c:', @FindCall, Self);
    end;
      

  9.   

    to 木石三
     看不太明白
    能不能写成函数啊,让我直接调用就可以了
    邮箱 [email protected]
      

  10.   

    将上面的改一下就完了:function FindCall(const Path, FileName: string; List: TStrings): Boolean; stdcall;
    var
      Ext: string;
    begin
      Result := True;
      Ext := ExtractFileExt(FileName);
      if SameText(Ext, '.abc') then 
      begin
        List.Add(Path + FileName);
        //List.Add(Format('Path: %s, FileName: %s', [Path, FileName]));
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      // 从C盘找文件(包括子目录),找到一个文件则调用FindCall函数(回调),  
      // Memo1.Lines为参数,对应FindCall的List参数。
      FindAllFile('c:', @FindCall, Memo1.Lines);
    end;