delphi下文件查找findfirst

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.

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
搜索TXT 文件的示例unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Vcl.ExtCtrls; type TForm1 = class(TForm) ListBox1: TListBox; Memo2: TMemo; Panel1: TPanel; Label1: TLabel; Label2: TLabel; Label3: TLabel; Edit1: TEdit; ButtonSearchFile: TButton; FolderPath: TEdit; FileExt: TEdit; ProgressBar1: TProgressBar; procedure ButtonSearchFileClick(Sender: TObject); procedure ListBox1Click(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } procedure SearchFile1(FileName: string; FindText: string); function MakeFileList(Path, FileExt: string): TStringList; function FileInUsed(FileName: TFileName): Boolean; public { Public declarations } end; var Form1: TForm1; implementation uses StrUtils; {$R *.dfm} { Search Options KeyWord in file FileName FileSize FileCreateTime FileModifyTime keyword filepath openfile found addListbox } var FileNamePathList, FileNameList: TStringList; procedure TForm1.FormCreate(Sender: TObject); begin FileNameList := TStringList.Create; FileNamePathList := TStringList.Create; end; { if FileInUsed ('D:\Administrator\Documents\MyProjects\FileSearch\Win32\Debug\Project1.exe') then ShowMessage('File is in use.') else ShowMessage('File not in use.'); } function TForm1.FileInUsed(FileName: TFileName): Boolean; var HFileRes: HFILE; begin Result := False; if not FileExists(FileName) then Exit; // 如果文件不存在,返回false HFileRes := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); Result := (HFileRes = INVALID_HANDLE_VALUE); if not Result then CloseHandle(HFileRes); end; procedure TForm1.SearchFile1(FileName: string; FindText: string); var SearchList: TStringList; begin try SearchList := TStringList.Create; if FileExists(FileName) and (not FileInUsed(FileName)) then begin SearchList.LoadFromFile(FileName); if Boolean(Pos(UpperCase(FindText), UpperCase(SearchList.Text))) then begin FileNameList.Add(ExtractFileName(FileName)); FileNamePathList.Add(FileName); end; end; finally SearchList.Free; end; end; procedure TForm1.ButtonSearchFileClick(Sender: TObject); var I, n: Integer; List: TStringList; begin try ButtonSearchFile.Caption := 'SearchFile'; List := TStringList.Create; List.Clear; FileNameList.Clear; FileNamePathList.Clear; List := MakeFileList(FolderPath.Text, FileExt.Text); ProgressBar1.Max := List.Count; for I := 0 to List.Count - 1 do begin Application.ProcessMessages; SearchFile1(List[I], Edit1.Text); ProgressBar1.Position := I; end; ListBox1.Items.Text := FileNameList.Text; ButtonSearchFile.Caption := IntToStr(FileNamePathList.Count) + ' 条'; finally List.Free; end; end; { 这个过程得显示进度 } function TForm1.MakeFileList(Path, FileExt: string): TStringList; var sch: TSearchrec; begin Result := TStringList.Create; if RightStr(Trim(Path), 1) '\' then Path := Trim(Path) + '\' else Path := Trim(Path); if not DirectoryExists(Path) then begin Result.Clear; Exit; end; if FindFirst(Path + '*', faAnyfile, sch) = 0 then begin repeat Application.ProcessMessages; if ((sch.Name = '.') or (sch.Name = '..')) then Continue; if DirectoryExists(Path + sch.Name) then begin Result.AddStrings(MakeFileList(Path + sch.Name, FileExt)); end else begin if (UpperCase(ExtractFileExt(Path + sch.Name)) = UpperCase(FileExt)) or (FileExt = '.*') then Result.Add(Path + sch.Name); end; until FindNext(sch) 0; FindClose(sch); end; end; procedure TForm1.ListBox1Click(Sender: TObject); var s: string; txt: string; begin if not FileExists(FileNamePathList[ListBox1.ItemIndex]) then Exit; Memo2.Lines.LoadFromFile(FileNamePathList[ListBox1.ItemIndex]); Caption := FileNamePathList[ListBox1.ItemIndex]; txt := Form1.Memo2.Text; if Boolean(Pos(UpperCase(Edit1.Text), UpperCase(txt))) then begin Memo2.SetFocus; Memo2.SelStart := Pos(UpperCase(Edit1.Text), UpperCase(txt)) - 1; Memo2.SelLength := Length(Edit1.Text); end; end; end.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值