Link to home
Start Free TrialLog in
Avatar of Dark_King
Dark_King

asked on

Read text file

I have a txt file look like this.

.jw1,jw1_file,"c:\windows\notepad.exe"
.lv2,jw1_file,"c:\windows\notepad.exe"
.jw2,jw2_file,"c:\windows\notepad.exe"
.lss,lss_file,"c:\app\ziper.exe"

I want to read line after line but capture position of “,”

First read set like this
Set1:=<.jw1>
Set2:=< jw1_file>
Set3:=<"c:\windows\notepad.exe">

Do something…..

Read next line and set next values from files.
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dygj
dygj

try using tstringlist and delimitedtext.

small ex here. You need to write code to read the file and loop trough all lines.
but this should give you a start.

var
b:tstringlist;
c,d,e:string;
begin
try
b:=tstringlist.create;
b:delimiter:=',';
b.delimitedtext:='add your line here, and create a loop handling all your lines';
// show each item
c:=b[0];
d:=b[1];
e:=b[2];
showmessage('Extention: '+c+' Type: '+d+'  File name:'+e)
finally
freeandnil(b);
end;
end;

Reply if you need more info about reading the file itselfs.
//................................ Unit ................................

unit Unit1_Q_21791945;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, Grids, StdCtrls, Buttons;

type
  TForm1 = class(TForm)
      MemoTxt: TMemo;
      Memo1: TMemo;
      Memo2: TMemo;
      Memo3: TMemo;
      BitBtn1: TBitBtn;
      procedure FormCreate(Sender: TObject);
      procedure BitBtn1Click(Sender: TObject);
    private
      FNT:    string;
    { Private declarations }
    public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FNT := ExtractFilePath(Application.ExeName) + 'Text.txt';
  MemoTxt.Lines.LoadFromFile(FNT);
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  I:      Integer;
  J:      Integer;
  K:      Integer;
  L:      Integer;
  LS:     Integer;
  ST:     string;
  SETS:   string;
  procedure Add_Line_into_Memo(K: Integer);
  begin
    SetLength(SETS, LS);
    case K of
      1: Memo1.Lines.Add(SETS);
      2: Memo2.Lines.Add(SETS);
      3: Memo3.Lines.Add(SETS);
    end;
  end;
begin
  for I := 0 to MemoTxt.Lines.Count-1 do
  begin
    K := 1;
    LS := 0;
    ST := MemoTxt.Lines[I];
    L := Length(ST);
    SetLength(SETS, L);
    for J := 1 to L do
      SETS[J] := #32;
    for J := 1 to L do
    begin
      if (ST[J]=',') then
      begin
        Add_Line_into_Memo(K);
        SetLength(SETS, L);
        LS := 0;
        Inc(K);
      end
      else
      begin
        Inc(LS);
        SETS[LS] := ST[J];
      end;
      if (J=L) then
        Add_Line_into_Memo(K);
    end;
  end;
  Memo1.Lines.SaveToFile('Set1.txt');
  Memo2.Lines.SaveToFile('Set2.txt');
  Memo3.Lines.SaveToFile('Set3.txt');
end;

end.

//................................ Form ................................

object Form1: TForm1
  Left = 216
  Top = 112
  BorderIcons = [biSystemMenu, biMinimize]
  BorderStyle = bsSingle
  Caption = 'Form1'
  ClientHeight = 542
  ClientWidth = 760
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object MemoTxt: TMemo
    Left = 4
    Top = 48
    Width = 256
    Height = 480
    TabOrder = 0
  end
  object Memo1: TMemo
    Left = 264
    Top = 48
    Width = 104
    Height = 480
    ReadOnly = True
    TabOrder = 1
  end
  object Memo2: TMemo
    Left = 372
    Top = 48
    Width = 136
    Height = 480
    ReadOnly = True
    TabOrder = 2
  end
  object Memo3: TMemo
    Left = 512
    Top = 48
    Width = 232
    Height = 480
    ReadOnly = True
    TabOrder = 3
  end
  object BitBtn1: TBitBtn
    Left = 176
    Top = 16
    Width = 80
    Height = 25
    Caption = 'Make Sets =>'
    TabOrder = 4
    OnClick = BitBtn1Click
  end
end