Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Find in Files (like Grep)

Posted on 2003-10-28
3
Medium Priority
?
731 Views
Last Modified: 2012-05-04
I need to be able to search through files (a mixture of Word, Powerpoint,PDF,Databases,Applications ) raplidly for a given string- similar to the Unix grep process. The files may contain just system code as well.

Has anyone any code to do this please, I will need to cope with files that may be open as well

Thanks
0
Comment
Question by:Linking
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:swift99
ID: 9638664
grep is actually shipped with Delphi   :o)

It is also an open source utility, so you should be able to lift the code you need directly from it so long as you adhere to the license agreement.

Worst case scenario, you just need to use the directory reading functions to traverse the directories.

see SearchRec, FindFirst, FindNext, and FindClose functions in the Delphi online help.  The example provided there should get you started.

For every file, you open the file as a file of char, and then you read through it one character at a time looking for a match to the characters you are looking for.

As you do this, several obvious optimizations will probably occur to you.
0
 
LVL 2

Accepted Solution

by:
alsantos earned 2000 total points
ID: 9638823
Hi Linking, You can use this code, It will search for a string in all files in directory(and subdirectory) you want and put at listbox the files that have the text in.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    ListBox1: TListBox;
    Button2: TButton;
    Edit3: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    FScanAborted: Boolean;
  public
    Function ScanDrive( root, filemask: String; Text2Find: String; hitlist: TStrings ): Boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TextExist(FileName: String;String2Find: String): Boolean;
Var
  MyStream: TMemoryStream;
  MyString: String;
  A: Integer;
begin
  Result := False;
  MyStream := TMemoryStream.Create;
  Try
    MyStream.LoadFromFile(FileName);
    SetLength(MyString, MyStream.Size);
    MyStream.ReadBuffer(Pointer(MyString)^,MyStream.Size);
    A := Pos(String2Find,MyString);
    If A <> 0 then
        Result:=True;
    Finally
     MyStream.Free;
    end;
end;

Function TForm1.ScanDrive( root, filemask: String; Text2Find: String; hitlist: TStrings ): Boolean;
  Function ScanDirectory( Var path: String ): Boolean;
    Var
      SRec: TSearchRec;
      pathlen: Integer;
      res: Integer;
    Begin
      label1.caption := path;
      pathlen:= Length(path);
      res := FindFirst( path+filemask, faAnyfile, SRec );
      If res = 0 Then
      try
        While res = 0 Do
         Begin
          if FileExists(path + SRec.Name) then
            if TextExist(path + SRec.Name, Text2Find) then
               hitlist.Add( path + SRec.Name );
          res := FindNext(SRec);
        End;
      finally
        FindClose(SRec)
      end;
      Application.ProcessMessages;
      Result := not (FScanAborted or Application.Terminated);
      If not Result Then Exit;
 
      res := FindFirst( path+'*.*', faDirectory, SRec );
      If res = 0 Then
      try
        While (res = 0) and Result Do Begin
          If ((Srec.Attr and faDirectory) = faDirectory) and
            (Srec.name[1] <> '.')
          Then Begin
            path := path + SRec.name + '\';
            Result := ScanDirectory( path );
            SetLength( path, pathlen );
          End;
          res := FindNext(SRec);
        End;
      finally
        FindClose(SRec)
      end;
    End;
Begin
  Screen.Cursor := crHourglass;
  try
    Result := ScanDirectory(root);
  finally
    Screen.Cursor := crDefault
  end;
End;

procedure TForm1.Button1Click(Sender: TObject);
begin
  listbox1.clear;
  ScanDrive(edit1.text, edit2.text, edit3.text, listbox1.items );
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 fScanAborted := True;
end;

end.

alsantos :)
0
 
LVL 6

Expert Comment

by:swift99
ID: 9639061
If you open the files for read shared then you should be good for most files, unless they are opened for exclusive use.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question