Re: data from text extraction


[ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by WAllison on December 06, 2001:

In Reply to: Re: data from text extraction posted by Rob Pouwelse on November 08, 2001:

: : I've got a question..:

: : how can i extract certain values (int's or string's) from a text file??

: : it's like this:
: : Nov 5 09:00:00 gateway newsyslog[14491]: logfile turned over //this is the first line
: : Nov 5 09:02:15 gateway sendmail[14502]: JAA14502: from=bounce-ntsysadmin-869560@lyris.sunbelt-software.com, size=2766, class=0, pri=32766, nrcpts=1, msgid=LYRIS-869560-19766-2001.11.05-03.01.37--J.Weijnen#matas.nl@lyris.sunbelt-software.com, proto=SMTP, relay=lyris.sunbelt-software.com [207.90.4.67]
: : Nov 5 09:02:16 gateway sendmail[14503]: JAA14502: to=jean, delay=00:00:02, xdelay=00:00:00, mailer=cyrus, stat=Sent

: : now i have to extract certain values (the ones behind the "=" but not all)
: : The lines are random in order...

: : I haven't the foggiest on how to do it..

: : please help..

: : tnx, in advance
: : Rob Pouwelse

:
: the from= and msgid= wouldn't post cause of /< and />
-------------------------------

I quickly came up with this - this way you need to know the keynames before the =.

GetDataString('F:\Source\TestApp\TestFile.txt', 0, 'from');

If i get time ill write one that goes through th file picking them automatically.

function TMainForm.GetDataString(FileName: string; LineNo: Integer; KeyName: string): string;
var F: TextFile;
    i, APos, BPos, CPos: integer;
    s, s1: string;
begin
  If KeyName[Length(KeyName)] <> '=' Then 
   KeyName := KeyName + '=';
  If FileExists(FileName) Then
   begin
    AssignFile(F, FileName);
    Reset(F);
    i := 0;
    while not (EOF(F)) and (i <> LineNo + 1) do
     begin
      Readln(F, s);
      i := i + 1;
     end;
    APos := Pos(Uppercase(KeyName), UpperCase(s));
    if APos > 0 Then
     begin
      APos := APos + Length(KeyName);
      s1 := Copy(s, APos, Length(s) - APos);
      BPos := Pos(',', s1);
      If BPos = 0 Then BPos := Length(s);
      CPos := APos + Pos(',', s1);
      s := Copy(s, APos, BPos - 1);
     end
    else s := '';
    CloseFile(F);
   end;
  Result := s;
end;
 


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]