Solved

Find a string in a Txt file

Posted on 2002-06-16
5
159 Views
Last Modified: 2010-04-04
Hello from Dinkleburger

Listen carefully what i want to do? lol

i have a File hidden away in C:\Windows\Sytem\FILENAME.Txt

the file sometimes is blank and sometimes has many lines of words

one word in the file is    ABRACADABRA

QUESTION... I want a procedure to read the File at its location and then search for the word ABRACADABRA
if the word is found then return a result TRUE.
FALSE if not found

Because i am not a good programmer yet i want the complete code
many thanks in advance Dinkleburger
0
Comment
Question by:Dinkleburger
5 Comments
 
LVL 7

Accepted Solution

by:
Motaz earned 50 total points
ID: 7081204
use this function:

function SearchWord(AWord, AFileName: string): Boolean;
var
  F: TextFile;
  Line: string;
begin
  Result:= False;
  if FileExists(AFileName) then
  begin
    AssignFile(F, AFileName);
    Reset(F);
    while not Eof(F) do
    begin
      Readln(F, Line);
      if Pos(AWord, Line) > 0 then // Word found
      begin
        Result:= True;
        Break;
      end; // if Pos..
    end; // while not Eof
    CloseFile(F);
  end; // if FileExists..
end;

and this is an example of how to use it:

  if SearchWord('ABRACADABRA', 'C:\Windows\Sytem\FILENAME.Txt') then
    ShowMessage('Found')
  else
    ShowMessage('Not found');

Motaz
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7081447
HI,
Here one of the possibilities:
function IsMyWordThere(const MyWord, FileName: string): boolean;
begin
  Result:=False;
//
  if not FileExists(FileName)
   then EXIT;
//
  with TStringList.Create do
    try
     LoadFromFile(FileName);
     Result:=Pos(MyWord, Text)>0;
    finally
      Free;
    end;
end;

Sincerely,
Nestorua.
0
 
LVL 3

Expert Comment

by:lopem
ID: 7081980
If you want speed you can use an hyperstring unit... Try www.torry.ru for many search strings units and components.

best regards,
Manuel Lopez (lopem)
0
 

Author Comment

by:Dinkleburger
ID: 7082055
Thank you everyone for excellent help
All the answers are very good but can only be one winner today *smiles* lol
First to the line today was Motaz
Thankyou all
regards Dinkleburger
0
 
LVL 7

Expert Comment

by:Motaz
ID: 7085142
Thanks Dinkleburger.

Motaz
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

867 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

21 Experts available now in Live!

Get 1:1 Help Now