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!

Multiple questions about Opening and Reading text files in Delphi 3

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
AU
Here is a question i hope one of you can answer it is a bit difficult to explain but i'll try anyway.

I wish to have a program which, on a single button click, will strip data from files in a given location and place it into a Access 2000 table.

I have broken the problem into five parts

1. Opening the file
2. Reading each line
3. Insertion into an Access 2000 database
4. Looping to do multiple files
5. Closing the files and the database once finished with





PART ONE - OPENING THE FILE
The location which i will be scanning for the files is static : c:\folder\files.dat

The files are all named the same except for a number at the end of the file name:

Qwerty00001.dat,
Qwerty00002.dat ...
Qwerty99999.dat

So i have used the following formula to do that.

CODE:

case FileNumber of
0..9: FileNo := '0000' + IntToStr(FileNumber);
10..99: FileNo := '000' + IntToStr(FileNumber);
100..999: FileNo := '00' + IntToStr(FileNumber);
1000..9999: FileNo := '0' + IntToStr(FileNumber);
else
FileNo := IntToStr(FileNumber);
end;

//So file number is a set 5 character length number 00001 to 99999

//The fileaddress is now going to be:

FileAddress := 'c:\folder\qwerty' + FileNo + '.dat';

//I have the file opening like so:

FileOpen(FileAddress,fmOpenRead);

Is this the best way of opening the said file.


PART TWO - READING EACH LINE
Now that the file is opened i want to be able to read each line in the file and see if the line is filled with needed data. The first two characters tell me if the line is data filled, if the characters are '20' then the line is needed.

If the file is needed then the program will have to strip out sections of the line for use.

SystemNo is the charcters 10 - 24 (length 15)
PhoneNo is the characters 44 - 64 (length 21)
Charge is the characters 128 - 133 (length 6)

Is is possible for the system to read each line and then after seeing if it is needed, extract the correct data.
There maybe more than ONE needed line in each file, but there may also be zero.

My current code doesn't work at all... It gives me the following "EInOutError" and "I/O error 6".

ReadLn(FileAddress, Ch);



PART THREE - INSERTION INTO AN ACCESS 2000 DATABASE
Now that i have gotten the data in the relevant fields (SystemNo, PageNo, Charge), i want to insert the data into an access table located at : 'c:\folder\database.mdb'

The database is passworded with the username/password combo being 'admin'/'q' {A nice easy password huh!}

The table is called ABC_Details and the table fields are SystemNo, PageNo, Charge.

Any ideas how to do this.





PART FOUR - LOOPING TO DO MULTIPLE FILES
At the moment i have the program looping using a While...Do loop

While FileNumber < 1000 do
begin
blah blah
end;

Is this the best way of doing it, or is there an even better way?




PART FIVE - CLOSING THE FILES AND DATABASE ONCE FINISHED WITH
After the program has gone through all of the files that are the folder, i want to close up ALL the opened '.dat' files and the Access Database.

How do you do this?



-----------------

Thankyou all for your help!
 
Okay PaidtheUmpire, here is the beef :

put on your form an ADOConnection component and an ADOTable component. point the ADOconnection to your .mdb file and link ADOtable to it and select the correct table.
I named your table component Tbl_data for this example.

then use this code :

procedure ExtractDataFromFile(Filename : string);


var F : TextFile;
Line : string;

begin
AssignFile(F,FileName);
Reset(F,1);
while not F.EOF do
begin
Readln(F,Line);
if Line <> '' then
begin
Tbl_data.Insert;
Tbl_data.Edit;
Tbl_data.FieldByName('SystemNo').AsString:=Copy(Line,10,15);
Tbl_data.FieldByName('PhoneNo').AsString:=Copy(Line,44,21);
Tbl_data.FieldByName('Charge').AsString:=Copy(Line,128,6);
Tbl_data.Post;
end;
end;
CloseFile(F);
end;

procedure ReadAllFiles;
{this is your main loop, call this from your buttonclick event}
var FileNr : integer;
FileName : string;

begin
for FileNr:=1 to 99999 do
begin
Filename:=Format('c:\folder\qwerty%.5d.dat',FileNr);
if FileExists(FileName) then
ExtractDataFromFile(Filename);
end;
end;

while this code is far from complete, I think it gives you an idea how to get started [spin]

greetings,

 
Thanks,

i'll get back to you on how it turns out!

Paid the umpire :)
 
Ok i am having a few problems with the above code, and the major one is that the program doesn't run if loop:

While Not f.oef do

is present, so i can only read the first line of the text file, is there a way around this problem? like a loop thing or something?
 
You said that &quot;While Not f.oef do&quot; doesn't run, but the actual line is &quot;While Not f.eof do&quot; with the &quot;e&quot; and &quot;o&quot; in the order of &quot;E)nd O)f F)ile&quot;. If this is not a typo in your message, then it is the problem in your code.

Good answer whosrdaddy, here's a star.
 
I saw a little typo in my code, correction is in bold font :

procedure ReadAllFiles;
{this is your main loop, call this from your buttonclick event}
var FileNr : integer;
FileName : string;

begin
for FileNr:=1 to 99999 do
begin
Filename:=Format('c:\folder\qwerty%.5d.dat',[FileNr]);
if FileExists(FileName) then
ExtractDataFromFile(Filename);
end;
end;


 
Sorry but both of these don't help me much.

Thanks again but,

when i do the
while not F.EOF do

.... code
it tells me


&quot;Record, Object or Class type required&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top