Re: data from text extraction


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

Posted by WAllison on December 06, 2001 at 03:04:09:

In Reply to: data from text extraction posted by Rob Pouwelse on November 08, 2001 at 14:43:19:

: 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=...
: 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..
-------------------

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


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