Solved

string search

Posted on 2000-03-20
12
380 Views
Last Modified: 2010-04-04
I want to build a small programme so that the user can search through a directory of files to find any files which contain a certain string, which he will type into a text box.
The file type could be any type of file except .exe and I do not want the file displayed on screen, just the file name when being searched, and true/false if it contains the search string.
Can this be done ?
0
Comment
Question by:pady
[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
  • 4
  • 3
  • 2
  • +2
12 Comments
 

Expert Comment

by:netbeto
ID: 2637156
Hy pady,
Aren't you try do make a existent program??? The "Find" on Windows XX can do that!

NetBeto.
0
 

Accepted Solution

by:
SuperSy earned 100 total points
ID: 2637190
FindFirst-FindNext-FindClose functions should do the trick.

Set the variable "Path" in FindFirst function to the directory you wish to search + a mask for the "certain string" you need to look for.  I think the path will be something like

DirString + '\*' + CertainString + '*';
(You will get something like 'C:\test\*abc*' for DirString = 'C:\test' and CertainString = 'abc')

You will get the filename as type TSearchRec.Name (a string type), so check if its extension is '.exe' or not (you can use ExtractFileExt() function), and you are set.  

All these functions are in SysUtils, so if you need help, it shouldn't be hard to find ... I think.  

Hope this works!

Sy
0
 
LVL 9

Expert Comment

by:ITugay
ID: 2637195
hi pady,

may it help you:
-----------

procedure TForm1.SpeedButton1Click(Sender: TObject);
var SRC : TSearchRec;
    Path : string;
    L    : TStringList;
begin
    L:=TStringList.Create;
    Path:='C:\';
    if FindFirst(Path+'*.*',faAnyFile,SRC) = 0 then
    repeat
      if (SRC.Attr <> faDirectory)
      and (SRC.Attr <> faVolumeID) then
      begin
        L.Clear;
        try
          L.LoadFromFile(Path+SRC.Name);
        except end;
        if pos('your string',L.Text) > 0
            then Listbox1.Items.Add(SRC.Name+' Yes')
            else Listbox1.Items.Add(SRC.Name+' No');
     end;
    until FindNext(SRC) <> 0;
    L.Free;
end;
-----

Need some comments?

Best regards,
Igor.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ITugay
ID: 2639300
to padi,

Sorry, my code will not work with binary files. The code for search string in binary file is:
-----------------
function FindInFile(aStr,aFileName : string) : integer;
var F : TFileStream;
    S : String;
begin
  F:=TFileStream.Create(aFileName,fmOpenRead);
  SetLength(S,F.Size);
  F.Read(pointer(S)^,F.Size);
  result:=pos(aStr,S);
  SetLength(S,0);
  F.Free;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
var SRC : TSearchRec;
begin
    if FindFirst('e:\projects\temp\*.*',faAnyFile,SRC) = 0 then
    repeat
       if (SRC.Attr <> faDirectory)
      and (SRC.Attr <> faVolumeID) then
       try
        Listbox1.Items.Add(SRC.Name+' '+IntToStr(FindInFile('project1','e:\projects\temp\'+SRC.Name)));
       except end;
    until findNext(SRC)<>0;
end;

----------------
The functionFindInFile return position+1 of aStr in file aFilename. If result is "0" then string not found.

Cheers,
Igor.

PS: be sure size of binary file is less then available memory.
0
 

Author Comment

by:pady
ID: 2645879
Netbeto is correct, Ive just found the facility in W95 to search for a string, I didnt know it was there.
many thanks for the work but there is no point in re-inventing the wheel
pady
0
 

Expert Comment

by:SuperSy
ID: 2645965
Whoa, then I don't want the points, man.  Give it to Netbeto.  
0
 

Expert Comment

by:netbeto
ID: 2646048
To SuperSy
That's a good idea!!!!!
0
 

Expert Comment

by:SuperSy
ID: 2646142
To netbeto:

Do you know how to do this (Transfer my Expert points to you)?
0
 

Author Comment

by:pady
ID: 2646158
sorry about the mixup fellas
0
 

Expert Comment

by:SuperSy
ID: 2646231
Don't worry --- there was not much you could do.  Actually I am the one causing a mess here, not you.  
0
 

Expert Comment

by:netbeto
ID: 2646262
Ok Guys,
Let forget that, I was just joking!!! Doesn't matter the points, what real matter is help ourself!! Don't is??

Cheers,
NetBeto.

0
 
LVL 3

Expert Comment

by:darinw
ID: 2651382
Hi all,

netbeto, please see the question I have posted for you in this topic area:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=10316516 

darinw
Customer Service
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi - replicating a form 8 112
TServerSocket - file via sendbuf or Text via sendtext ? 1 92
JAudiorecorder record freezing the app 29 93
Wincontrol not (correctly) drawn 15 59
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

752 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