Solved

string search

Posted on 2000-03-20
12
365 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from 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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 89
Find and Replace Stream with 0s 8 62
Moving (cutting/pasting) controls in a TTabbedNotebook... 7 34
Firemonkey allowing RTL on android 6 31
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

776 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