高级查找
unit uMain;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TfrMain = class(TForm)
pnl1: TPanel;
mmo1: TMemo;
pnl2: TPanel;
lbl1: TLabel;
ed1: TEdit;
ed2: TEdit;
btn1: TButton;
chk1: TCheckBox;
lbl2: TLabel;
lblNumberFound: TLabel;
ed3: TEdit;
lbl3: TLabel;
procedure btn1Click(Sender: TObject);
private
procedure FileSearch(const PathName, FileName : string; const InDir : boolean);
public
{ Public declarations }
end;
var
frMain: TfrMain;
implementation
{$R *.DFM}
uses uFileInfo;
procedure TfrMain.FileSearch(const PathName, FileName : string; const InDir : boolean);
var Rec : TSearchRec;
Path : string;
sl: TStringList;
ii: integer;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
begin
//判断是否包含字符串
sl := TStringList.Create;
sl.LoadFromFile(Path + Rec.Name);
for ii:=0 to sl.Count-1 do
begin
if Pos(trim(ed3.text), sl.Strings[ii]) > 0 then
begin
mmo1.Lines.Add(Path + Rec.Name);
//mmo1.Lines.Add(Copy(sl.Strings[ii], Pos(trim(ed3.text), sl.Strings[ii]), 5));
BREAK;
end;
end;
end;
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
If not InDir then Exit;
if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then
FileSearch(Path + Rec.Name, FileName, True);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
end;
procedure TfrMain.btn1Click(Sender: TObject);
begin
mmo1.Clear;
lblNumberFound.Caption:=Inttostr(mmo1.lines.Count) + ' files found.';
FileSearch(Ed1.Text, Ed2.Text, Chk1.State in [cbChecked]);
lblNumberFound.Caption:=Inttostr(mmo1.lines.Count) + ' files found.';
SHOWMESSAGE('OK');
end;
end.
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;
type
TfrMain = class(TForm)
pnl1: TPanel;
mmo1: TMemo;
pnl2: TPanel;
lbl1: TLabel;
ed1: TEdit;
ed2: TEdit;
btn1: TButton;
chk1: TCheckBox;
lbl2: TLabel;
lblNumberFound: TLabel;
ed3: TEdit;
lbl3: TLabel;
procedure btn1Click(Sender: TObject);
private
procedure FileSearch(const PathName, FileName : string; const InDir : boolean);
public
{ Public declarations }
end;
var
frMain: TfrMain;
implementation
{$R *.DFM}
uses uFileInfo;
procedure TfrMain.FileSearch(const PathName, FileName : string; const InDir : boolean);
var Rec : TSearchRec;
Path : string;
sl: TStringList;
ii: integer;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileName, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
begin
//判断是否包含字符串
sl := TStringList.Create;
sl.LoadFromFile(Path + Rec.Name);
for ii:=0 to sl.Count-1 do
begin
if Pos(trim(ed3.text), sl.Strings[ii]) > 0 then
begin
mmo1.Lines.Add(Path + Rec.Name);
//mmo1.Lines.Add(Copy(sl.Strings[ii], Pos(trim(ed3.text), sl.Strings[ii]), 5));
BREAK;
end;
end;
end;
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
If not InDir then Exit;
if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name<>'.') and (Rec.Name<>'..') then
FileSearch(Path + Rec.Name, FileName, True);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
end;
procedure TfrMain.btn1Click(Sender: TObject);
begin
mmo1.Clear;
lblNumberFound.Caption:=Inttostr(mmo1.lines.Count) + ' files found.';
FileSearch(Ed1.Text, Ed2.Text, Chk1.State in [cbChecked]);
lblNumberFound.Caption:=Inttostr(mmo1.lines.Count) + ' files found.';
SHOWMESSAGE('OK');
end;
end.