Results 1 to 12 of 12

Thread: Please help, problems with reading numerous textfiles

  1. #1

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718

    Unhappy 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.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863
    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!

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.

  4. #4

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718

    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.

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.

  6. #6
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.    
    5.     Dim intX                As Integer
    6.     Dim strCurrFileName     As String
    7.     Dim intCurrFileHandle   As Integer
    8.     Dim astrFileFields()    As String
    9.     Dim intFieldNbr         As Integer
    10.     Dim objCurrLI           As ListItem
    11.            
    12.     SetupListView
    13.        
    14.     Do
    15.         intX = intX + 1
    16.         ' The Do loop will exit (and files will stop loading) as soon
    17.         ' as it is discovered that a particular file is not there.
    18.         ' This assumes the files are sequentially numbered with no gaps.
    19.         ' If this assumption is not correct, you will need to adjust the
    20.         ' code to handle the situation (i.e., skip the missing file and
    21.         ' continue on to the next) ...
    22.         strCurrFileName = "p:\fleets\estimates\" & CStr(intX) & ".txt"
    23.         If Dir(strCurrFileName) = "" Then
    24.             Exit Do
    25.         End If
    26.         ' get a Windows file handle
    27.         intCurrFileHandle = FreeFile
    28.         ' Open the current file
    29.         Open strCurrFileName For Input As #intCurrFileHandle
    30.        
    31.         'clear the array which will store the current file's fields
    32.         Erase astrFileFields
    33.         intFieldNbr = -1
    34.        
    35.         Do Until EOF(intCurrFileHandle)
    36.             ' Based on your post, each "field" of the text file is on a
    37.             ' separate line. I am assuming "<CONTAINS>" and "<END CONTAINS>"
    38.             ' are also there. Anyway, this code will store each line (field)
    39.             ' of the file in an element of the astrFileFields array ...
    40.             intFieldNbr = intFieldNbr + 1
    41.             ReDim Preserve astrFileFields(0 To intFieldNbr)
    42.             Line Input #intCurrFileHandle, astrFileFields(intFieldNbr)
    43.         Loop
    44.    
    45.         Close intCurrFileHandle
    46.        
    47.         ' Now add a listitem in the listview for this file's data ...
    48.         ' It is assumed that we want to NOT load the "CONTAINS" and "END CONTAINS" lines.
    49.         ' Basically, as long you know what order the fields came in on in the file, you
    50.         ' can assign them to the appropriate column of the listview.
    51.         Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
    52.         objCurrLI.SubItems(1) = astrFileFields(2)
    53.         objCurrLI.SubItems(2) = astrFileFields(3)
    54.        
    55.     Loop
    56.    
    57.    
    58. End Sub
    59.  
    60. Private Sub SetupListView()
    61.  
    62.     With ListView1
    63.         .View = lvwReport
    64.         .GridLines = True
    65.         .ColumnHeaders.Clear
    66.         .ListItems.Clear
    67.         ' set up 3 columns, each having width 1/3 the size of
    68.         ' the ListView itself ...
    69.         .ColumnHeaders.Add , , "Estimate #", .Width * 0.33
    70.         .ColumnHeaders.Add , , "Reg #", .Width * 0.33
    71.         .ColumnHeaders.Add , , "Date", .Width * 0.33
    72.     End With
    73.  
    74. 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.

  7. #7

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718

    Talking 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.

  8. #8
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    As far as skipping over files that are not there, here's how you would modify the previous code ...
    VB Code:
    1. Private Sub Form_Load()
    2.    
    3.     Dim intX                As Integer
    4.     Dim strCurrFileName     As String
    5.     Dim intCurrFileHandle   As Integer
    6.     Dim astrFileFields()    As String
    7.     Dim intFieldNbr         As Integer
    8.     Dim objCurrLI           As ListItem
    9.            
    10.     SetupListView
    11.  
    12.     ' Use a "reasonable" limit here. I chose 2000. Recall that
    13.     ' the limit for an Integer is 32767; for more, a Long must
    14.     ' must be used ...
    15.     For intX = 1 To 2000
    16.         strCurrFileName = "p:\fleets\estimates\" & CStr(intX) & ".txt"
    17.         If Dir(strCurrFileName) = "" Then
    18.             ' do nothing - file is not there
    19.         Else
    20.             ' Process the file ...
    21.             ' get a Windows file handle
    22.             intCurrFileHandle = FreeFile
    23.             ' Open the current file
    24.              Open strCurrFileName For Input As #intCurrFileHandle
    25.        
    26.             'clear the array which will store the current file's fields
    27.             Erase astrFileFields
    28.             intFieldNbr = -1
    29.            
    30.             Do Until EOF(intCurrFileHandle)
    31.                 ' Based on your post, each "field" of the text file is on a
    32.                 ' separate line. I am assuming "<CONTAINS>" and "<END CONTAINS>"
    33.                 ' are also there. Anyway, this code will store each line (field)
    34.                 ' of the file in an element of the astrFileFields array ...
    35.                 intFieldNbr = intFieldNbr + 1
    36.                 ReDim Preserve astrFileFields(0 To intFieldNbr)
    37.                 Line Input #intCurrFileHandle, astrFileFields(intFieldNbr)
    38.             Loop
    39.        
    40.             Close intCurrFileHandle
    41.            
    42.             ' Now add a listitem in the listview for this file's data ...
    43.             ' It is assumed that we want to NOT load the "CONTAINS" and "END CONTAINS" lines.
    44.             ' Basically, as long you know what order the fields came in on in the file, you
    45.             ' can assign them to the appropriate column of the listview.
    46.             Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
    47.             objCurrLI.SubItems(1) = astrFileFields(2)
    48.             objCurrLI.SubItems(2) = astrFileFields(3)
    49.            
    50.     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:
    1. Set objCurrLI = ListView1.ListItems.Add(, , astrFileFields(1))
    2.             objCurrLI.SubItems(1) = astrFileFields(2)
    3.             objCurrLI.SubItems(2) = astrFileFields(3)
    You could add:
    VB Code:
    1. 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.

  9. #9

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    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?

  10. #10
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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.

  11. #11

    Thread Starter
    Fanatic Member VisionIT's Avatar
    Join Date
    Nov 2002
    Location
    Workin'...
    Posts
    718
    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.

  12. #12
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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
  •  



Click Here to Expand Forum to Full Width