Solved

string search

Posted on 2000-03-20
12
371 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi component that can load a DLL in design time? 8 74
Delphi application Soap connection 5 120
Working with hours 3 65
Dynamically Created Query 3 62
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 this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

856 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