Solved

string search

Posted on 2000-03-20
12
354 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
  • 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now