Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

string search

Posted on 2000-03-20
12
Medium Priority
?
393 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 300 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
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!

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Integration Management Part 2
Suggested Courses

916 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