Solved

Find a string in a Txt file

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

17 Experts available now in Live!

Get 1:1 Help Now