Solved

Find a string in a Txt file

Posted on 2002-06-16
5
162 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
[X]
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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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