Solved

Find a string in a Txt file

Posted on 2002-06-16
5
160 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

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.

Question has a verified solution.

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

Suggested Solutions

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…
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…
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