|
-
Nov 14th, 2002, 08:03 AM
#1
Thread Starter
Fanatic Member
Please help, problems with reading numerous textfiles
Hi all.
I am currently working on an estimating program which writes files according to their job no. IE
User enters Job ID: 1001
it then becomes...
Filename:1001.txt
<CONTAINS>
1001
registration no
date
and so on...
<END CONTAINS>
All the writing parts are done without any major probs, but i need the program to print all the files in a form. IE
It open ALL files in the estimates directory (1001.txt,1002.txt,1003.txt) etc, and prints them like this:
ESTNO: Registration No: Date:
1001 BU51 ??? 13/11/02
1002 VU02 ??? 14/11/02
1003 V270 ??? 13/10/02
I cannot do it! HELP! Anyone any ideas?
Cheers
Paul.
-
Nov 14th, 2002, 08:35 AM
#2
Whre is your problem?
Opening the files?
Reading the files?
Writng the contents onto a form?
Multiline writing?
Do want to wright in a label or what?
You're welcome to rate this post!
If your problem is solved, please use the Mark thread as resolved button
Wait, I'm too old to hurry!
-
Nov 14th, 2002, 09:01 AM
#3
This shouldn't be too difficult. As Opus said, please provide some more details as to what you want (or where you are having trouble), and myself or others can surely help you with the code.
"It's cold gin time again ..."
Check out my website here.
-
Nov 14th, 2002, 09:21 AM
#4
Thread Starter
Fanatic Member
Please help, problems with reading numerous textfiles
Thanx for the reply 
The problem is...
i have hundreds of text files in a directory called p:\fleets\estimates. All the files are named:
1.txt
2.txt
3.txt
upto currently
1001.txt
I need the form to read all the files (1.txt, 2.txt, 3.txt) and their contents at the same time, and input the contents of each file into their respective boxes. Here is an example...
The filename is: 1001.txt
The contents are:
1001 (the estimate no)
BU02 ??? (the registration number)
13/11/02 (the date of input)
and so on...
the next file would be...
The filename: 1002.txt
The contents are:
1002 (the estimate no)
VU51 ??? (the registration number)
and so on...
When the user wants to view the entire inventory, they can open a form called "displayestimates" and on_load... it should read the above into this format...
Estimate No: Registration No: Date:
1001 BU02 13/11/02 (1001.txt)
1002 VU51 12/11/02 (1002.txt)
Sorry to blind you all with this... but i'm really desperate. Thanx again
Paul.
-
Nov 14th, 2002, 09:25 AM
#5
OK, shouldn't be too bad. I need to put out some fires here at work, but then I can cook up some code for you. I'll be back a little later ...
"It's cold gin time again ..."
Check out my website here.
-
Nov 14th, 2002, 10:40 AM
#6
OK, here we go. I am using the ListView control to display the data. (If you haven't used it before, you're in for a treat.)
To run my test, open a new VB project and from Project -> Components, check Microsoft Windows Common Controls 6.0 to bring the ListView into the project.
Put a ListView on the form, and paste in the following code:
VB Code:
Option Explicit
Private Sub Form_Load()
Dim intX As Integer
Dim strCurrFileName As String
Dim intCurrFileHandle As Integer
Dim astrFileFields() As String
Dim intFieldNbr As Integer
Dim objCurrLI As ListItem
SetupListView
Do
intX = intX + 1
' The Do loop will exit (and files will stop loading) as soon
' as it is discovered that a particular file is not there.
' This assumes the files are sequentially numbered with no gaps.
' If this assumption is not correct, you will need to adjust the
' code to handle the situation (i.e., skip the missing file and
' continue on to the next) ...
strCurrFileName = "p:\fleets\estimates\" & CStr(intX) & ".txt"
If Dir(strCurrFileName) = "" Then
Exit Do
End If
' get a Windows file handle
intCurrFileHandle = FreeFile
' Open the current file
Open strCurrFileName For Input As #intCurrFileHandle
'clear the array which will store the current file's fields
Erase astrFileFields
intFieldNbr = -1
Do Until EOF(intCurrFileHandle)
' Based on your post, each "field" of the text file is on a
' separate line. I am assuming "<CONTAINS>" and "<END CONTAINS>"
' are also there. Anyway, this code will store each line (field)
' of the file in an element of the astrFileFields array ...
intFieldNbr = intFieldNbr + 1
ReDim Preserve astrFileFields(0 To intFieldNbr)
Line Input #intCurrFileHandle, astrFileFields(intFieldNbr)
Loop
Close intCurrFileHandle
' Now add a listitem in the listview for this file's data ...
' It is assumed that we want to NOT load the "CONTAINS" and "END CONTAINS" lines.
' Basically, as long you know what order the fields came in on in the file, you
' can assign them to the appropriate column of the listview.
Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
objCurrLI.SubItems(1) = astrFileFields(2)
objCurrLI.SubItems(2) = astrFileFields(3)
Loop
End Sub
Private Sub SetupListView()
With ListView1
.View = lvwReport
.GridLines = True
.ColumnHeaders.Clear
.ListItems.Clear
' set up 3 columns, each having width 1/3 the size of
' the ListView itself ...
.ColumnHeaders.Add , , "Estimate #", .Width * 0.33
.ColumnHeaders.Add , , "Reg #", .Width * 0.33
.ColumnHeaders.Add , , "Date", .Width * 0.33
End With
End Sub
I tested this and it worked. The comments in the code indicate my assumptions, please adjust accordingly if I assumed wrong. Let me know how this works for you.
"It's cold gin time again ..."
Check out my website here.
-
Nov 14th, 2002, 12:35 PM
#7
Thread Starter
Fanatic Member
CHEERS !
Thanks for all your help... it works a treat.
I'm just trying to figure out how to:
1. Print the form because form1.printform doesn't print the data, only the box itself.
2. How to change the script over so it skips over file No's which don't exist. That will be a tough one ! lol
Anyway... thanks again. I'll bookmark you as "The GURU"!
Tara.
Paul.
-
Nov 14th, 2002, 01:03 PM
#8
As far as skipping over files that are not there, here's how you would modify the previous code ...
VB Code:
Private Sub Form_Load()
Dim intX As Integer
Dim strCurrFileName As String
Dim intCurrFileHandle As Integer
Dim astrFileFields() As String
Dim intFieldNbr As Integer
Dim objCurrLI As ListItem
SetupListView
' Use a "reasonable" limit here. I chose 2000. Recall that
' the limit for an Integer is 32767; for more, a Long must
' must be used ...
For intX = 1 To 2000
strCurrFileName = "p:\fleets\estimates\" & CStr(intX) & ".txt"
If Dir(strCurrFileName) = "" Then
' do nothing - file is not there
Else
' Process the file ...
' get a Windows file handle
intCurrFileHandle = FreeFile
' Open the current file
Open strCurrFileName For Input As #intCurrFileHandle
'clear the array which will store the current file's fields
Erase astrFileFields
intFieldNbr = -1
Do Until EOF(intCurrFileHandle)
' Based on your post, each "field" of the text file is on a
' separate line. I am assuming "<CONTAINS>" and "<END CONTAINS>"
' are also there. Anyway, this code will store each line (field)
' of the file in an element of the astrFileFields array ...
intFieldNbr = intFieldNbr + 1
ReDim Preserve astrFileFields(0 To intFieldNbr)
Line Input #intCurrFileHandle, astrFileFields(intFieldNbr)
Loop
Close intCurrFileHandle
' Now add a listitem in the listview for this file's data ...
' It is assumed that we want to NOT load the "CONTAINS" and "END CONTAINS" lines.
' Basically, as long you know what order the fields came in on in the file, you
' can assign them to the appropriate column of the listview.
Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
objCurrLI.SubItems(1) = astrFileFields(2)
objCurrLI.SubItems(2) = astrFileFields(3)
Next intX
As far as printing, I never use "PrintForm". For this app, I would just use the good "old fashioned" BASIC-type print statements using the Printer object. You would have to program your own page breaks, page counts, headings, and all that junk. However, to print the detail data, you could put in statements at the end of the loop, after you populate the listview. For example, after
VB Code:
Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
objCurrLI.SubItems(1) = astrFileFields(2)
objCurrLI.SubItems(2) = astrFileFields(3)
You could add:
VB Code:
Printer.Print astrFileFields(1), astrFileFields(2), astrFileFields(3)
One thing to remember using this - when you are done (after the loop is over), be sure to add this statement:
Printer.EndDoc
Good luck!
"It's cold gin time again ..."
Check out my website here.
-
Nov 14th, 2002, 03:04 PM
#9
Thread Starter
Fanatic Member
Hello again m8y.
Just tried the modified text for the skipping files bit... and it errors on the last line
next intx
I just copied like for like exactly what you have printed, only changed the path to the files a little. Any ideas?
-
Nov 14th, 2002, 03:11 PM
#10
Sorry, I am missing an "End If". Place that just before the "Next intX" line.
"It's cold gin time again ..."
Check out my website here.
-
Nov 14th, 2002, 03:27 PM
#11
Thread Starter
Fanatic Member
thanx pal.
Printing bit next, thanks for all your help.
I couldn't of finished it without your help.
Tara.
P.S If you ever need a discount P.C, gimmie a bell.
Paul.
-
Nov 14th, 2002, 03:31 PM
#12
Glad to help. Good luck with the rest of the project.
"It's cold gin time again ..."
Check out my website here.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|