Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I extract specific info from a .TXT file? 2

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
ZA
Can some guru out there please tell me how I can get Delphi to read the contents of a .TXT file in search of specific groups of words?

For instance .. I have several different .TXT files which have one of (say) the following combinations (somewhere) in it and I need to identify them. Along with (varying) details behind them Such as ...

REGISTER EMR ABC123456
REGISTER SM DEF789012 .

The trailing combinations of characters (up to 45 long) vary from one .TXT file to the next.


 
Here is a routine that I have used to read a .csv file and load a TStringGrid. If you weed out all of the statements that you don't need (and calls to routines you don't have) you should be left with a skeleton outline of a piece of code you can adapt for processing text files.
Code:
[green]
Code:
{Load a string grid from a csv file.}
[/color]
Code:
procedure LoadGridFromCSV( AGrid:TStringGrid; FileName:string );
var
  f: TextFile;
  s: string;
  WorkList: TStringList;
  nRow: integer;
begin
  WorkList := TStringList.Create;
  AssignFile(f, FileName);
  Reset(f);
  with AGrid do begin
    if not Eof(f) then begin
[green]
Code:
      // Process first record (column headers)
[/color]
Code:
      ReadLn(f,s);
      CSVToStringList( S, WorkList );
      ColCount := WorkList.Count;
      RowCount := 2;
      Rows[0].Assign(WorkList);
      nRow := 0;
      While not Eof(f) do begin
        Readln(f, S);
        CSVToStringList( S, WorkList );
        nRow := nRow + 1;
        if nRow >= RowCount then RowCount := (nRow+1);
        Rows[nRow].Assign(WorkList);
      end;
[green]
Code:
 {while}
[/color]
Code:
    end;
[green]
Code:
 {if}
[/color]
Code:
  end;
[green]
Code:
 {with}
[/color]
Code:
  CloseFile(f);
  WorkList.Free;
  GridColAutoSize( AGrid );
end;
 
This routine allows you to pass in a string list of keys to look for and get back the "key=value" pairs.

You could change the code to delete any non-matched key fron the AValueList;
Code:
function FindValuesInFile(AFileName : String; AValueList : TStrings) : Boolean;
var
   TheseStrings : TStringList;
   ValueCounter, ItemCounter : Integer;
   ThisValue, ThisString : String;
begin
   Result := False;
   TheseStrings := TStringList.Create;
   try
       TheseStrings.LoadFromFile(AFileName);
       for ValueCounter := 0 to AValueList.Count - 1 do
       begin
           ThisValue := AValueList[ValueCounter];
           for ItemCounter := 0 to TheseStrings.Count - 1 do
           begin
               ThisString := TheseStrings[ItemCounter];
               if SameText(ThisValue + ' ', Copy(ThisString, 1, Length(ThisValue) + 1)) then
               begin
                   Delete(ThisString, 1, Length(ThisValue) + 1);
                   AValueList.Values[ThisValue] := Trim(ThisString);
                   Result := True;
                   Break;
               end;
           end;
       end;
   finally
       TheseStrings.Free;
   end;
   
end;

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top