hulken
asked on
Read from a text-file.
I Have a text-file that comes from a exel document.
It looks like this:
04-8103225100 1
04-8109225237 3
04-8114225000 4
04-8101225000/P 4
04-8107225225 1
04-8102225000 1
04-8107225110 1
04-8111225180 1
04-8111225160 1
04-452070/B 16
04-452012/B 8
04-450020/B 16
04-450020/B 8
04-457200/I 2
04-457200/I 1
04-452092/B 32
04-452092/B 16
04-452231/A 6
04-452233/A 1
04-452233/A 4
04-452240/A 8
04-452283/A 4
04-332225 1
04-332005 1
04-690004 1
The column to the left is a article number and the column to the right is a number.
Each line of the text-file must not contain text.
The left column comes from the first column in the exel file and the right column from the second so I dont know if it is a tab between or if it's just spaces.
I like to make a routin that
opens a textfile like the one abowe
and add to a list-view.
so it looks like this:
art-no no_of
-----------------------
04-8103225100 1
04-8109225237 3
04-8114225000 4
04-8101225000/P 4
04-8107225225 1
04-8102225000 1
04-8107225110 1
Can someone help me with this...
I can split this question in smaller one so you get more points...
It looks like this:
04-8103225100 1
04-8109225237 3
04-8114225000 4
04-8101225000/P 4
04-8107225225 1
04-8102225000 1
04-8107225110 1
04-8111225180 1
04-8111225160 1
04-452070/B 16
04-452012/B 8
04-450020/B 16
04-450020/B 8
04-457200/I 2
04-457200/I 1
04-452092/B 32
04-452092/B 16
04-452231/A 6
04-452233/A 1
04-452233/A 4
04-452240/A 8
04-452283/A 4
04-332225 1
04-332005 1
04-690004 1
The column to the left is a article number and the column to the right is a number.
Each line of the text-file must not contain text.
The left column comes from the first column in the exel file and the right column from the second so I dont know if it is a tab between or if it's just spaces.
I like to make a routin that
opens a textfile like the one abowe
and add to a list-view.
so it looks like this:
art-no no_of
-----------------------
04-8103225100 1
04-8109225237 3
04-8114225000 4
04-8101225000/P 4
04-8107225225 1
04-8102225000 1
04-8107225110 1
Can someone help me with this...
I can split this question in smaller one so you get more points...
function resolve(String strInput):String;
begin
i,iMid:integer;
str:String;
strList:TStringList;
strList:=TStringList.creat e;
for i:=0 to Length(strInput) do
begin
str:=copy(strInput,i,1);
if str=' ' then
begin
iMid=i;
break;
end;
end;
strList.add(trim(copy(strI nput,0,iMi d)));
strList.add(trim(copy(strI nput,iMid, Length(str Input)-iMi d)));
result:=strList.text;
end;
//procedure add to listview
var
tempList,strList : TStringList;
index: integer;
ListItem: TListItem;
begin
tempList := TStringList.create;
strList := TStringList.create;
if FileExists(strFilePath) then
begin
tempList.LoadFromFile(strF ilePath);
index := 0;
ListView1.items.clear;
while (index < tempList .count) do
begin
ListItem:=ListView1.Items. Add;//???u
strList.text:=rosolve(temp List.Strin gs[index]) ;
ListItem.Caption:=tempList .Strings[0 ];
ListItem.SubItems.Add(temp List.Strin gs[1]);
inc(index);
end;
end;
tempList.free;
strList.free;
end;
begin
i,iMid:integer;
str:String;
strList:TStringList;
strList:=TStringList.creat
for i:=0 to Length(strInput) do
begin
str:=copy(strInput,i,1);
if str=' ' then
begin
iMid=i;
break;
end;
end;
strList.add(trim(copy(strI
strList.add(trim(copy(strI
result:=strList.text;
end;
//procedure add to listview
var
tempList,strList : TStringList;
index: integer;
ListItem: TListItem;
begin
tempList := TStringList.create;
strList := TStringList.create;
if FileExists(strFilePath) then
begin
tempList.LoadFromFile(strF
index := 0;
ListView1.items.clear;
while (index < tempList .count) do
begin
ListItem:=ListView1.Items.
strList.text:=rosolve(temp
ListItem.Caption:=tempList
ListItem.SubItems.Add(temp
inc(index);
end;
end;
tempList.free;
strList.free;
end;
function resolve(String strInput):String;
begin
i,iMid:integer;
str:String;
strList:TStringList;
strList:=TStringList.creat e;
for i:=0 to Length(strInput) do
begin
str:=copy(strInput,i,1);
if str=' ' then
begin
iMid=i;
break;
end;
end;
strList.add(trim(copy(strI nput,0,iMi d)));
strList.add(trim(copy(strI nput,iMid, Length(str Input)-iMi d)));
result:=strList.text;
end;
//procedure add to listview
var
tempList,strList : TStringList;
index: integer;
ListItem: TListItem;
begin
tempList := TStringList.create;
strList := TStringList.create;
if FileExists(strFilePath) then
begin
tempList.LoadFromFile(strF ilePath);
index := 0;
ListView1.items.clear;
while (index < tempList .count) do
begin
ListItem:=ListView1.Items. Add;//???u
strList.text:=rosolve(temp List.Strin gs[index]) ;
ListItem.Caption:=tempList .Strings[0 ];
ListItem.SubItems.Add(temp List.Strin gs[1]);
inc(index);
end;
end;
tempList.free;
strList.free;
end;
begin
i,iMid:integer;
str:String;
strList:TStringList;
strList:=TStringList.creat
for i:=0 to Length(strInput) do
begin
str:=copy(strInput,i,1);
if str=' ' then
begin
iMid=i;
break;
end;
end;
strList.add(trim(copy(strI
strList.add(trim(copy(strI
result:=strList.text;
end;
//procedure add to listview
var
tempList,strList : TStringList;
index: integer;
ListItem: TListItem;
begin
tempList := TStringList.create;
strList := TStringList.create;
if FileExists(strFilePath) then
begin
tempList.LoadFromFile(strF
index := 0;
ListView1.items.clear;
while (index < tempList .count) do
begin
ListItem:=ListView1.Items.
strList.text:=rosolve(temp
ListItem.Caption:=tempList
ListItem.SubItems.Add(temp
inc(index);
end;
end;
tempList.free;
strList.free;
end;
well my version :-)
unit lv_fill_file_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//a generic stringsplitter
function SplitStr(Var AString : String; ADelim : String) : String;
var APos : Integer;
begin
Result := '';
APos := Pos(ADelim,AString);
if APos > 0 then
begin
Result := Copy(AString,1,APos-1);
AString := copy(Astring,APos+length(A delim),Max LongInt);
end
else
begin
Result := AString;
AString := '';
end;
end;
//the filling routine
procedure do_fill_lv(AFileName : String; AListView : TListView);
var
sl : TStringList;
I : Integer;
s : String;
li : TListItem;
begin
sl := TStringList.Create;
Try
try
sl.loadfromfile(AFileName) ;
AListView.Items.Clear;
for i := 0 to sl.count - 1 do
begin
s := sl[i];
li := AlistView.Items.Add;
li.Caption := SplitStr(s,' '); //five blanks as delimiter
li.SubItems.Add(s);
end;
finally
sl.free;
end;
except
raise;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
if opendialog1.execute then
do_fill_lv(opendialog1.fil ename,list view1);
end;
end.
meikl ;-)
unit lv_fill_file_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//a generic stringsplitter
function SplitStr(Var AString : String; ADelim : String) : String;
var APos : Integer;
begin
Result := '';
APos := Pos(ADelim,AString);
if APos > 0 then
begin
Result := Copy(AString,1,APos-1);
AString := copy(Astring,APos+length(A
end
else
begin
Result := AString;
AString := '';
end;
end;
//the filling routine
procedure do_fill_lv(AFileName : String; AListView : TListView);
var
sl : TStringList;
I : Integer;
s : String;
li : TListItem;
begin
sl := TStringList.Create;
Try
try
sl.loadfromfile(AFileName)
AListView.Items.Clear;
for i := 0 to sl.count - 1 do
begin
s := sl[i];
li := AlistView.Items.Add;
li.Caption := SplitStr(s,' '); //five blanks as delimiter
li.SubItems.Add(s);
end;
finally
sl.free;
end;
except
raise;
end;
end;
procedure TForm1.Button1Click(Sender
begin
if opendialog1.execute then
do_fill_lv(opendialog1.fil
end;
end.
meikl ;-)
corected version, because the blank lines:
unit lv_fill_file_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function SplitStr(Var AString : String; ADelim : String) : String;
var APos : Integer;
begin
Result := '';
APos := Pos(ADelim,AString);
if APos > 0 then
begin
Result := Copy(AString,1,APos-1);
AString := copy(Astring,APos+length(A delim),Max LongInt);
end
else
begin
Result := AString;
AString := '';
end;
end;
procedure do_fill_lv(AFileName : String; AListView : TListView);
var
sl : TStringList;
I : Integer;
s : String;
li : TListItem;
begin
sl := TStringList.Create;
Try
try
sl.loadfromfile(AFileName) ;
AListView.Items.Clear;
for i := 0 to sl.count - 1 do
begin
s := sl[i];
if trim(s) <> '' then
begin
li := AlistView.Items.Add;
li.Caption := SplitStr(s,' '); //five blanks as delimiter
li.SubItems.Add(s);
end;
end;
finally
sl.free;
end;
except
raise;
end;
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
if opendialog1.execute then
do_fill_lv(opendialog1.fil ename,list view1);
end;
end.
meikl ;-)
unit lv_fill_file_u;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
ListView1: TListView;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function SplitStr(Var AString : String; ADelim : String) : String;
var APos : Integer;
begin
Result := '';
APos := Pos(ADelim,AString);
if APos > 0 then
begin
Result := Copy(AString,1,APos-1);
AString := copy(Astring,APos+length(A
end
else
begin
Result := AString;
AString := '';
end;
end;
procedure do_fill_lv(AFileName : String; AListView : TListView);
var
sl : TStringList;
I : Integer;
s : String;
li : TListItem;
begin
sl := TStringList.Create;
Try
try
sl.loadfromfile(AFileName)
AListView.Items.Clear;
for i := 0 to sl.count - 1 do
begin
s := sl[i];
if trim(s) <> '' then
begin
li := AlistView.Items.Add;
li.Caption := SplitStr(s,' '); //five blanks as delimiter
li.SubItems.Add(s);
end;
end;
finally
sl.free;
end;
except
raise;
end;
end;
procedure TForm1.Button1Click(Sender
begin
if opendialog1.execute then
do_fill_lv(opendialog1.fil
end;
end.
meikl ;-)
ASKER
kretzschmar,
With your solution I get all
items in the same column in the list-view.
http://130.236.233.202/Test.zip
or
http://130.236.233.202/test.txt
Will cointain a example of the file I like to import.
With your solution I get all
items in the same column in the list-view.
http://130.236.233.202/Test.zip
or
http://130.236.233.202/test.txt
Will cointain a example of the file I like to import.
ASKER
kretzschmar,
Your solution seems to work fine if I replace:
li.Caption := SplitStr(s,' '); //five blanks as delimiter
with
li.Caption := SplitStr(s,#9); //Tab as delimiter
But how can I change this to work eaven if it's more than one Tab or if the columns are separated with one or more spaces?
Your solution seems to work fine if I replace:
li.Caption := SplitStr(s,' '); //five blanks as delimiter
with
li.Caption := SplitStr(s,#9); //Tab as delimiter
But how can I change this to work eaven if it's more than one Tab or if the columns are separated with one or more spaces?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just for fun, another version:
procedure TextToListView(const AName: string; AView: TListView);
var
Line: Integer;
S: string;
function CutStringPart: string;
var
SB,SE: Integer;
begin
SB:=1;
while (SB<=Length(S)) and (S[SB]<=#32) do //detect leading spaces
Inc(SB);
SE:=SB;
while (SE<=Length(S)) and (S[SE]>#32) do //trace text part
Inc(SE);
Result:=Copy(S,SB,SE-SB);
while (SE<=Length(S)) and (S[SE]<=#32) do //find trailering spaces
Inc(SE);
Delete(S,1,SE-1);
end;
begin
with TStringList.Create do try
LoadFromFile(AName);
AView.Items.Clear;
for Line:=0 to Count-1 do begin
S:=Strings[Line];
if S<>'' then
with AView.Items.Add do begin
Caption:=CutStringPart;
while S<>'' do
SubItems.Add(CutStringPart );
end;
end;
finally
Free;
end;
end;
procedure TextToListView(const AName: string; AView: TListView);
var
Line: Integer;
S: string;
function CutStringPart: string;
var
SB,SE: Integer;
begin
SB:=1;
while (SB<=Length(S)) and (S[SB]<=#32) do //detect leading spaces
Inc(SB);
SE:=SB;
while (SE<=Length(S)) and (S[SE]>#32) do //trace text part
Inc(SE);
Result:=Copy(S,SB,SE-SB);
while (SE<=Length(S)) and (S[SE]<=#32) do //find trailering spaces
Inc(SE);
Delete(S,1,SE-1);
end;
begin
with TStringList.Create do try
LoadFromFile(AName);
AView.Items.Clear;
for Line:=0 to Count-1 do begin
S:=Strings[Line];
if S<>'' then
with AView.Items.Add do begin
Caption:=CutStringPart;
while S<>'' do
SubItems.Add(CutStringPart
end;
end;
finally
Free;
end;
end;
good points,
let me try it too :-)
var
L: TStringList;
I,J: Integer;
S: String;
begin
ListView1.Items.Clear;
L := TStringList.Create;
try
L.LoadFromFile('D:\FILE1.T XT');
for I := 0 to L.Count-1 do
begin
S := Trim(L[I]);
if S <> '' then
begin
J := Pos(' ', S);
with ListView1.Items.Add do
begin
Caption := Trim(Copy(S, 1, J-1));
Subitems.Add(Trim(Copy(S, J, 255)));
end
end
end
finally
L.Free
end
end;
-----
Igor
let me try it too :-)
var
L: TStringList;
I,J: Integer;
S: String;
begin
ListView1.Items.Clear;
L := TStringList.Create;
try
L.LoadFromFile('D:\FILE1.T
for I := 0 to L.Count-1 do
begin
S := Trim(L[I]);
if S <> '' then
begin
J := Pos(' ', S);
with ListView1.Items.Add do
begin
Caption := Trim(Copy(S, 1, J-1));
Subitems.Add(Trim(Copy(S, J, 255)));
end
end
end
finally
L.Free
end
end;
-----
Igor
:-)) many ways possible
If you are get the text file from excel document.
There are a component that deal with excel documents.
This will be more accurate to read your data.
It's name is XLSReadWrite and it's URL http://www.axolot.com
Thanks
Ahmed Ragab
There are a component that deal with excel documents.
This will be more accurate to read your data.
It's name is XLSReadWrite and it's URL http://www.axolot.com
Thanks
Ahmed Ragab
? may this will be an answer, if there is only this textfile with no chance to access the excel-sheet directly from which this will come?
i will recommend you to with draw your answer!
if hulken have the possibility to access excel directly,
then of course it would be a suggestion of many others.
let hulken decide, what do best fit to his needs
meikl ;-)
i will recommend you to with draw your answer!
if hulken have the possibility to access excel directly,
then of course it would be a suggestion of many others.
let hulken decide, what do best fit to his needs
meikl ;-)
:-) with draw -> withdraw
as i had seen, your suggestion is comercial
as i had seen, your suggestion is comercial
my last two comments are pointd to RAGAB2000
;-)
;-)
My original solution will only work for comma & space delimited text files, though it wouldn't be hard to alter. Classes.pas has this function def. for GetCommaText (the parser behind TStrings.commaText):
function TStrings.GetCommaText: string;
var
S: string;
P: PChar;
I, Count: Integer;
begin
Count := GetCount;
if (Count = 1) and (Get(0) = '') then
Result := '""'
else
begin
Result := '';
for I := 0 to Count - 1 do
begin
S := Get(I);
P := PChar(S);
while not (P^ in [#0..' ','"',',']) do P := CharNext(P);
if (P^ <> #0) then S := AnsiQuotedStr(S, '"');
Result := Result + S + ',';
end;
System.Delete(Result, Length(Result), 1);
end;
end;
You'd just need to modify:
(P^ in [#0..' ','"',','])
to contain the char(s) you do want to delimit by., for ex:
(P^ in [#0..' ','"',',',#9])
GL
Mike
function TStrings.GetCommaText: string;
var
S: string;
P: PChar;
I, Count: Integer;
begin
Count := GetCount;
if (Count = 1) and (Get(0) = '') then
Result := '""'
else
begin
Result := '';
for I := 0 to Count - 1 do
begin
S := Get(I);
P := PChar(S);
while not (P^ in [#0..' ','"',',']) do P := CharNext(P);
if (P^ <> #0) then S := AnsiQuotedStr(S, '"');
Result := Result + S + ',';
end;
System.Delete(Result, Length(Result), 1);
end;
end;
You'd just need to modify:
(P^ in [#0..' ','"',','])
to contain the char(s) you do want to delimit by., for ex:
(P^ in [#0..' ','"',',',#9])
GL
Mike
Another thought, if you'd prefer not to mess with your own parsing routines (though the above is a very simple mod) is to use the nthword funtion (in PSock, so you need the fastnet components, though I wouldn't recomend using them for inet stuff ;P ). It'll take a string, an arbritary delimiter & give the n'th word from the input string.
GL
Mike
GL
Mike
ASKER
as I said... it was a textfile I like to work with..
ASKER
OK,
edy, coffeetea , : ITugay and : AvonWyss
Thnks for helping me out.. I will add some questions with name points for XXX (where XXX is your name) to give you some points for helping me to learn how to work with text-files.
: kretzschmar:
I fell for youw solutions so I will accept your comment as an answer.. Thanks!
: RAGAB2000
Your propose was not what I asked for but mabe I will have some need for your comment lateron so I will add a question with points for you also.
Thnks every one for helping me out...
You are realy nice all of you...
I will probably see you here lateron.
edy, coffeetea , : ITugay and : AvonWyss
Thnks for helping me out.. I will add some questions with name points for XXX (where XXX is your name) to give you some points for helping me to learn how to work with text-files.
: kretzschmar:
I fell for youw solutions so I will accept your comment as an answer.. Thanks!
: RAGAB2000
Your propose was not what I asked for but mabe I will have some need for your comment lateron so I will add a question with points for you also.
Thnks every one for helping me out...
You are realy nice all of you...
I will probably see you here lateron.
ASKER
Thanks for the help.
well, thanks for the points :-)
glad you like it and its usefull for you,
glad to helped you :-)
good luck again
meikl ;-)
glad you like it and its usefull for you,
glad to helped you :-)
good luck again
meikl ;-)
var
sl : TStringList;
ix : integer;
begin
sl := TStringList.create;
sl.loadFromFile('some_file
ix := 0;
while (ix < sl.count) do
if sl[ix] = '' then
sl.delete(ix)
else
ix := ix+1;
sl.commaText := sl.text;
for ix := 0 to sl.count div 2 do
with listView1.items.add do
begin
caption := sl[ix*2];
subItems.add(sl[ix*2+1];
end;
sl.free;
end;
GL
Mike