I have a similar need to search through source files, and sometimes outside
the IDE is easier - call me old but I like doing things at the CMD prompt.
The Gexperts search is excellent too.
I installed the Findfile component of Zarko Gajic (Delphi About.com) (it's a
wrapper for the standard Delphi file find code) and to test it wrote a
search program that does exactly the sort of thing I need...starting folder,
file spec (eg *.pas) checkbox for subfolders, and a search string. Puts the
results in a StringGrid. Works fine, though repeated searches show there is
a bug somewhere with a pointer error exception on exit - of course that
mustn't be in my code? Free giveaway to any good home if you want a quick
and simple search - (let me know if you find what the bug is).
Source of main unit here - let me know if you want the DFM etc
unit Dfind1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, FindFile;
type
TForm1 = class(TForm)
FindFile1: TFindFile;
StringGrid1: TStringGrid;
Label1: TLabel;
edtFolder: TEdit;
Label2: TLabel;
edtFileSpec: TEdit;
Label3: TLabel;
edtString: TEdit;
chkbxSubFolders: TCheckBox;
lblFile: TLabel;
procedure CMDialogKey(Var Msg: TWMKEY); message CM_DIALOGKEY;
procedure FormActivate(Sender: TObject);
procedure edtStringExit(Sender: TObject);
procedure ShowFileList;
procedure Search;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
strlist:TStringList;
FindString:string;
numtotal,numinfile:integer;
filematchno:integer;
implementation
uses jkblib;
{$R *.DFM}
procedure Tform1.CMDialogKey(var Msg: TWMKEY);
{
This converts ENTER key messages to TAB key messages, provided a few
conditions hold
}
begin
if (ActiveControl is TCustomEdit)
or (ActiveControl is TCheckBox)
// or (ActiveControl is TPageControl)
// or (ActiveControl is TDBLookupComboBox)
// or (ActiveControl is TRadioButton)
// or (ActiveControl is TStringGrid)
then
begin
if Msg.CharCode = VK_RETURN then
begin
Msg.CharCode := VK_TAB;
// KeyDir:='Next';
end;
if Msg.Charcode = VK_ESCAPE then
begin
Msg.CharCode := VK_TAB;
// KeyDir:='Prev';
end;
end;
inherited;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
edtFolder.text:=FindFile1.Path;
edtFileSpec.text:=FindFile1.FileMask;
chkbxSubFolders.checked:=FindFile1.inSubFolders;
edtString.text:='';
end;
procedure TForm1.edtStringExit(Sender: TObject);
begin
Search;
end;
procedure TForm1.Search;
var
i,fileptr,lno,posn,grdptr:integer;
t:TextFile;
Filename:string;
line:string;
atotal:string;
shortname:string;
begin
FindFile1.FileMask:=edtFileSpec.text;
FindFile1.Path:=edtFolder.text;
FindFile1.InSubfolders:=chkbxSubFolders.checked;
findstring:=edtString.text;
//build filelist
strlist:=TStringList.create;
lblFile.caption:='searching for filenames';
Application.processmessages;
strlist:=FindFile1.SearchforFiles;
// ShowFileList; //(for testing)
// exit;
//clear grid
for I := 0 to StringGrid1.RowCount - 1 do
StringGrid1.Rows[I].Clear();
//now search for files
lblFile.caption:='searching files..';
Application.processmessages;
numtotal:=0;
filematchno:=0;
grdptr:=-1;
atotal:=inttostr(strlist.count);
for Fileptr:=0 to strlist.Count-1 do
begin
numinfile:=0;
lno:=0;
filename:=strlist.Strings[Fileptr];
if not FileExists(filename) then continue;
shortname:=filename;
if length(filename)>80 then
shortname:=copy(filename,1,20)+'..'+copy(filename,length(filename)-20,20);
//long path and filenames can crash the label
lblFile.caption:=inttostr(fileptr)+' '+shortname+' of '+atotal;
Application.processmessages;
assignfile(t,Filename);
reset(t);
while not eof(t) do
begin
readln(t,line);
inc(lno);
xcstrin(line,FindString,posn,1);
//above is equivalent to
//posn:=ansipos(FindString,line);
//but is not case sensitive
if posn>0 then
begin
inc(numinfile);
inc(numtotal);
if numinfile=1 then
begin
inc(grdptr);
inc(filematchno);
stringgrid1.cells[1,grdptr]:=Filename;
// Application.processmessages;
end;
inc(grdptr);
stringgrid1.cells[0,grdptr]:=inttostr(lno);
stringgrid1.cells[1,grdptr]:=line;
stringgrid1.rowcount:=grdptr+1;
if numtotal > 20 then stringgrid1.toprow:=grdptr-20; //to scroll
to bottom of list as add
end; //found
end; //not eof
closefile(t);
end; //end files
lblFile.caption:='Total files='+inttostr(strlist.count)
+' matched files='+inttostr(Filematchno)
+' matches='+inttostr(numtotal);
strlist.free;
stringgrid1.row:=grdptr;
stringgrid1.setfocus;
end;
procedure TForm1.ShowFileList;
var
I,Fileptr,grdptr:integer;
begin
//clear grid
for I := 0 to StringGrid1.RowCount - 1 do
StringGrid1.Rows[I].Clear();
for Fileptr:=0 to strlist.Count-1 do
begin
Stringgrid1.cells[0,Fileptr]:=inttostr(fileptr);
Stringgrid1.cells[1,Fileptr]:=strlist.Strings[Fileptr];
end;
Stringgrid1.Rowcount:=strlist.count;
Showmessage('This is the file list');
end;
end.
John
_______________________________________________
Offtopic mailing list
[email protected]
http://ns3.123.co.nz/mailman/listinfo/offtopic