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.
LVL 6
Dark_KingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pierre CorneliusCommented:
try this:

pas:
===
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Panel1: TPanel;
    Label1: TLabel;
    inpFilename: TEdit;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    btnRead: TButton;
    procedure Button1Click(Sender: TObject);
    procedure btnReadClick(Sender: TObject);
  private
    procedure GetTextData(fname: String);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute
    then inpFilename.Text:= OpenDialog1.FileName;
end;

procedure TForm1.GetTextData(fname: String);
var f: Textfile;
    s, set1, set2, set3: string;
    p: integer;
begin
  AssignFile(f, fname);
  reset(f);
  try
    while not eof(f) do
    begin
      readln(f, s);

      p:= pos(',', s);
      set1:= copy(s, 1, p-1);
      Delete(s, 1, p);

      p:= pos(',', s);
      set2:= copy(s, 1, p-1);
      Delete(s, 1, p);

      set3:= s;

      with ListView1.Items.Add do
      begin
        caption:= set1;
        SubItems.Add(set2);
        SubItems.Add(set3);
      end;
    end;
  finally
    closefile(f);
  end;
end;

procedure TForm1.btnReadClick(Sender: TObject);
begin
  if not FileExists(inpFilename.Text) then
    raise Exception.Create('Invlaid filename: '+inpFilename.Text);

  GetTextData(inpFilename.Text);
end;

end.


dfm:
=====
object Form1: TForm1
  Left = 192
  Top = 114
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 0
    Top = 41
    Width = 688
    Height = 405
    Align = alClient
    Columns = <
      item
        AutoSize = True
        Caption = 'Set1'
      end
      item
        AutoSize = True
        Caption = 'Set2'
      end
      item
        AutoSize = True
        Caption = 'Set3'
      end>
    TabOrder = 0
    ViewStyle = vsReport
  end
  object Panel1: TPanel
    Left = 0
    Top = 0
    Width = 688
    Height = 41
    Align = alTop
    TabOrder = 1
    DesignSize = (
      688
      41)
    object Label1: TLabel
      Left = 16
      Top = 8
      Width = 40
      Height = 13
      Caption = 'Input file'
    end
    object inpFilename: TEdit
      Left = 72
      Top = 8
      Width = 505
      Height = 21
      Anchors = [akLeft, akTop, akRight]
      TabOrder = 0
    end
    object Button1: TButton
      Left = 582
      Top = 7
      Width = 22
      Height = 22
      Anchors = [akTop, akRight]
      Caption = '…'
      Font.Charset = ANSI_CHARSET
      Font.Color = clWindowText
      Font.Height = -11
      Font.Name = 'Times New Roman'
      Font.Style = [fsBold]
      ParentFont = False
      TabOrder = 1
      OnClick = Button1Click
    end
    object btnRead: TButton
      Left = 616
      Top = 8
      Width = 68
      Height = 25
      Caption = 'Read'
      TabOrder = 2
      OnClick = btnReadClick
    end
  end
  object OpenDialog1: TOpenDialog
    Left = 304
  end
end


Regards
Pierre

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dygjCommented:
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.
esoftbgCommented:
//................................ 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.