Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Find a string in a Txt file

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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 parse string to params 3 136
Reconfigure Delphi Install? 2 60
tidtcpserver connection lost handle 2 102
how to resize animated Gif image in delphi ? 1 35
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 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