How to Read From 2 Seperate Files, and Write Data to 1

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kimmelsd33
    New Member
    • Dec 2009
    • 29

    How to Read From 2 Seperate Files, and Write Data to 1

    I am having trouble getting my code to work properly. I am opening 2 seperate files, skipping through the header info, and copying the columns of data into named variables. Then, I am printing the data to a file in the order of taking data from both files, and putting them into 1. I'm not sure where I am messing up, but it is not working properly. The code is as follows:
    Code:
    Open "C:\Temp.dat" For Append As #1
                Set fso = CreateObject("Scripting.FileSystemObject")
                Set objstream = fso.opentextfile(tFile, 1, False, 0) 'open selected file to read from
    'delete header
                        Dim i As Integer
                            i = 0
                        Do While i < 32 And Not objstream.AtEndOfStream
                            strLine = objstream.Readline
                            i = i + 1
                        Loop
    Do While Not objstream.AtEndOfStream
                            strLine = TrimAll(objstream.Readline)
                            yourarray = Split(strLine, " ") 'use tab to create space
                                'read into variables
                                depth = Format(Trim(yourarray(0)), "0")
                                If Trim(yourarray(1)) = "0.0000" Then
                                  fph = "0.0"
                                Else
                                  fph = Format(Trim(yourarray(1)), "0.0")
                                End If
                                If Trim(yourarray(2)) = "0.0000" Then
                                  mpf = "0.00"
                                Else
                                  mpf = Format(Trim(yourarray(2)), "0.00")
                                End If
                                If Trim(yourarray(3)) = "0.0000" Then
                                  slide = "0.0"
                                Else
                                  slide = Format(Trim(yourarray(3)), "0.0")
                                End If
                                If Trim(yourarray(4)) = "0.0000" Then
                                  pdc = "0.0"
                                Else
                                  pdc = Format(Trim(yourarray(4)), "0.0")
                                End If
                                If Trim(yourarray(5)) = "0.0000" Then
                                  tgas = "0"
                                Else
                                  tgas = Format(Trim(yourarray(5)), "0")
                                End If
                                If Trim(yourarray(6)) = "0.0000" Then
                                  c1 = "0"
                                Else
                                  c1 = Format(Trim(yourarray(6)), "0")
                                End If
                                If Trim(yourarray(7)) = "0.0000" Then
                                  c2 = "0"
                                Else
                                  c2 = Format(Trim(yourarray(7)), "0")
                                End If
                                If Trim(yourarray(8)) = "0.0000" Then
                                  c3 = "0"
                                Else
                                  c3 = Format(Trim(yourarray(8)), "0")
                                End If
                                If Trim(yourarray(9)) = "0.0000" Then
                                  c4i = "0"
                                Else
                                  c4i = Format(Trim(yourarray(9)), "0")
                                End If
                                If Trim(yourarray(10)) = "0.0000" Then
                                  c4n = "0"
                                Else
                                  c4n = Format(Trim(yourarray(10)), "0")
                                End If
                                If Trim(yourarray(11)) = "0.0000" Then
                                  mwin = "0"
                                Else
                                  mwin = Format(Trim(yourarray(11)), "0.0")
                                End If
                                If Trim(yourarray(12)) = "0.0000" Then
                                  mwout = "0"
                                Else
                                  mwout = Format(Trim(yourarray(12)), "0.0")
                                End If
                                If Trim(yourarray(13)) = "0.0000" Then
                                  mtmpin = "0"
                                Else
                                  mtmpin = Format(Trim(yourarray(13)), "0")
                                End If
                                If Trim(yourarray(14)) = "0.0000" Then
                                  mtmpout = "0"
                                Else
                                  mtmpout = Format(Trim(yourarray(14)), "0")
                                End If
                        Loop
    'read from LITH file
                            Set fso = CreateObject("Scripting.FileSystemObject")
                            Set objstream = fso.opentextfile(lithFile, 1, False, 0) 'open selected file to read from
    'delete header
                                Dim c As Integer
                                    c = 0
                                Do While c < 1 And Not objstream.AtEndOfStream
                                    strLine = objstream.Readline
                                    c = c + 1
                                Loop
                                Do While Not objstream.AtEndOfStream
                                    strLine = Trim(objstream.Readline)
                                    myarray = Split(strLine, vbTab) 'use tab to create space
                                        'read into variables
                                        lithdepth = Format(Trim(myarray(0)), "0")
                                        shale = Format(Trim(myarray(1)), "0")
                                        silt = Trim(myarray(2))
                                        sand = Trim(myarray(3))
                                        chalk = Trim(myarray(4))
                                        lime = Trim(myarray(5))
                                        dolo = Trim(myarray(6))
                                        anhy = Trim(myarray(7))
                                        coal = Trim(myarray(8))
                                        chert = Trim(myarray(9))
                                        nosamp = Trim(myarray(10))
                                 Loop
    'print data to file in specific order
    Print #1, depth & vbTab & fph & vbTab & mpf & vbTab & slide & vbTab & pdc & vbTab & tgas _
                                & vbTab & c1 & vbTab & c2 & vbTab & c3 & vbTab & c4i & vbTab & c4n & vbTab & shale _
                                & vbTab & silt & vbTab & sand & vbTab & chalk & vbTab & lime & vbTab & dolo & vbTab & anhy _
                                & vbTab & coal & vbTab & chert & vbTab & nosamp _
                                & vbTab & mwin & vbTab & mwout & vbTab & mtmpin & vbTab & mtmpout
    'close open files
    Close #1
                            fso = objstream.Close
                    fso = objstream.Close
    Now, it is sort of working, as far as, placing the data in the order in which I need it to be placed. But I am only getting 1 line of code and not multiple lines. Does anyone have some insight? Thanks in advance
    Attached Files
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    dear,

    The first loop reads all the lines from the first file => so the array holds only the last line
    the second loop reads the lines from the second file => so the array holds only the last line.

    You must read line by line (from the first and second file) and print in the same loop.

    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim c As Integer
       Open App.Path & "\Temp.dat" For Append As #1
    '§ open selected file to read from
       Set fso = CreateObject("Scripting.FileSystemObject")
       Set objstream_Curve = fso.opentextfile(App.Path & "\Curve.txt", 1, False, 0)
    '§ delete header
       i = 0
       Do While i < 32 And Not objstream_Curve.AtEndOfStream
          strLine = objstream_Curve.Readline
          i = i + 1
       Loop
    '§ read from LITH file
       Set fso = CreateObject("Scripting.FileSystemObject")
    '§ open selected file to read from
       Set objstream_LithCurves = fso.opentextfile(App.Path & "\LithCurves.txt", 1, False, 0)
    '§ delete header
       c = 0
       Do While c < 1 And Not objstream_LithCurves.AtEndOfStream
          strLine = objstream_LithCurves.Readline
          c = c + 1
       Loop
    '§ read line by line from Curve and Lithcurves
       Do While Not objstream_Curve.AtEndOfStream
          strLine = Trim(objstream_Curve.Readline)
          Array_Curve = Split(strLine, " ") 'use tab to create space
    '§ read into variables
          depth = Format(Trim(Array_Curve(0)), "0")
          If Trim(Array_Curve(1)) = "0.0000" Then
             fph = "0.0"
          Else
            fph = Format(Trim(Array_Curve(1)), "0.0")
          End If
          If Trim(Array_Curve(2)) = "0.0000" Then
            mpf = "0.00"
          Else
            mpf = Format(Trim(Array_Curve(2)), "0.00")
          End If
          If Trim(Array_Curve(3)) = "0.0000" Then
            slide = "0.0"
          Else
            slide = Format(Trim(Array_Curve(3)), "0.0")
          End If
          If Trim(Array_Curve(4)) = "0.0000" Then
            pdc = "0.0"
          Else
            pdc = Format(Trim(Array_Curve(4)), "0.0")
          End If
          If Trim(Array_Curve(5)) = "0.0000" Then
            tgas = "0"
          Else
            tgas = Format(Trim(Array_Curve(5)), "0")
          End If
          If Trim(Array_Curve(6)) = "0.0000" Then
            c1 = "0"
          Else
            c1 = Format(Trim(Array_Curve(6)), "0")
          End If
          If Trim(Array_Curve(7)) = "0.0000" Then
            c2 = "0"
          Else
            c2 = Format(Trim(Array_Curve(7)), "0")
          End If
          If Trim(Array_Curve(8)) = "0.0000" Then
            c3 = "0"
          Else
            c3 = Format(Trim(Array_Curve(8)), "0")
          End If
          If Trim(Array_Curve(9)) = "0.0000" Then
            c4i = "0"
          Else
            c4i = Format(Trim(Array_Curve(9)), "0")
          End If
          If Trim(Array_Curve(10)) = "0.0000" Then
            c4n = "0"
          Else
            c4n = Format(Trim(Array_Curve(10)), "0")
          End If
          If Trim(Array_Curve(11)) = "0.0000" Then
            mwin = "0"
          Else
            mwin = Format(Trim(Array_Curve(11)), "0.0")
          End If
          If Trim(Array_Curve(12)) = "0.0000" Then
            mwout = "0"
          Else
            mwout = Format(Trim(Array_Curve(12)), "0.0")
          End If
          If Trim(Array_Curve(13)) = "0.0000" Then
            mtmpin = "0"
          Else
            mtmpin = Format(Trim(Array_Curve(13)), "0")
          End If
          If Trim(Array_Curve(14)) = "0.0000" Then
            mtmpout = "0"
          Else
            mtmpout = Format(Trim(Array_Curve(14)), "0")
          End If
          strLine = Trim(objstream_LithCurves.Readline)
          Array_LithCurves = Split(strLine, vbTab) 'use tab to create space
             'read into variables
             lithdepth = Format(Trim(Array_LithCurves(0)), "0")
             shale = Format(Trim(Array_LithCurves(1)), "0")
             silt = Trim(Array_LithCurves(2))
             sand = Trim(Array_LithCurves(3))
             chalk = Trim(Array_LithCurves(4))
             lime = Trim(Array_LithCurves(5))
             dolo = Trim(Array_LithCurves(6))
             anhy = Trim(Array_LithCurves(7))
             coal = Trim(Array_LithCurves(8))
             chert = Trim(Array_LithCurves(9))
             nosamp = Trim(Array_LithCurves(10))
    'print data to file in specific order
       Print #1, depth & vbTab & fph & vbTab & mpf & vbTab & slide & vbTab & _
          pdc & vbTab & tgas & vbTab & c1 & vbTab & c2 & vbTab & c3 & vbTab & _
          c4i & vbTab & c4n & vbTab & shale & vbTab & silt & vbTab & _
          sand & vbTab & chalk & vbTab & lime & vbTab & dolo & vbTab & _
          anhy & vbTab & coal & vbTab & chert & vbTab & nosamp & vbTab & _
          mwin & vbTab & mwout & vbTab & mtmpin & vbTab & mtmpout
        Loop
    'close open files
       Close #1
       fso = objstream_LithCurves.Close
       fso = objstream_Curve.Close
    End Sub
    I hope this will help

    br,

    Comment

    • kimmelsd33
      New Member
      • Dec 2009
      • 29

      #3
      This is working like it should, but why is it that not all the data is in tab delimited format? The file it prints to has large gaps in it? Thanks for the help though. Nice job.

      Comment

      • kimmelsd33
        New Member
        • Dec 2009
        • 29

        #4
        Well, I said it was working like it should. It's not. I get an error #62. Input past end of file. The line:
        Code:
        Do While Not objstream_Curve.AtEndOfStream
              strLine = Trim(objstream_Curve.Readline) '<----this line
              Array_Curve = Split(strLine, " ") 'use tab to create space
        had to be edited to read like this:
        Code:
        Do While Not objstream_Curve.AtEndOfStream
              strLine = TrimAll(objstream_Curve.Readline) '<---edited line
              Array_Curve = Split(strLine, " ") 'use tab to create space
        because of a function I called as TrimAll. This is the only way I could read the lines, and then split them by vbTab. I believe with my file size, this is creating a problem when it starts reading from the LithCurves.txt file. The reason I believe is that I am calling the split as a vbTab delimiter. How can I resolve this issue? Thanks again

        Comment

        • vb5prgrmr
          Recognized Expert Contributor
          • Oct 2009
          • 305

          #5
          Okay, recieved your PM...

          and I can see where you are having your problems and possibly creating more for yourself.

          1st, I would suggest that you do your operations on one file at a time...
          2nd, get rid of the fso and use the code I showed you previously on how to open the file and read all of its contents in...(reason, fso creates another dependancy and it is slower than the native methods I have shown you)
          Code:
          'you remember this code don't you???
          Open FName For Input As #FNumb
          FileContents = Input(FileLen(FName), #FNumb)
          Close #FNumb
          LineArray = Split(FileContents, vbNewLine)
          
          'new part to move the contents you want to the beginning of the array
          For ForLoopCounter = NumberOfLinesToSkip - 1 To UBound(LineArray)
            MoveIndex = ForLoopCounter - (NumberOfLinesToSkip - 1)
            LineArray(MoveIndex) = LineArray(ForLoopCounter)
          Next ForLoopCounter
          
          'now redim so we only have the information we want...
          Redim Preserve LineArray(UBound(LineArray) - (NumberOfLinesToSkip - 1))
          
          'now all of your output variables need to be arrays themselves and since you do not know how many lines you read in, you will first need to dim them as empty arrays ()
          ReDim fph(UBound(LineArray))
          '....
          Okay from here you would then need to loop through splitting the linearray on vbtab and set the output variables to the contents (you have part of this code already in previous posts and in your own post above...).

          Then do the same for the other file...

          Now, what happens if the number of lines are not equal to each other??? (Problem I was mentioning above)

          So you will have to figure what you are going to do if the lines do not equal each other as you output...
          Code:
          Open FName For OutPut as #FNumb
          For ForLoopCounter = 0 To UBound(fph)
            Print #FNumb, fph(ForLoopCounter) & vbTab '....
          Next ForLoopCounter
          Which means in this output loop above or before it, you will need to see if the lines equal or not, and if not, which set of arrays will have more information. Then when you reach the limit of the one that has less information, what do you do? Do you stop the output and close the file? Do you put dummy values in and continue to the end of the information?

          Well anyway in which you do it, I would suggest that you build your output string before you print it so the print line itself would only be print #FNumb, OutPutString. (and easy to put a debug.print outputstring in for debugging purposes...)


          Oh, and in case you were wondering why your code does not work, is because you are doing this...

          Open For Output
          Open For Input
          Loop through file till the end is reached
          Open For Input
          Loop through file till the end is reached
          Print the result of the last line in each file
          close the output file



          Good Luck

          Comment

          • kimmelsd33
            New Member
            • Dec 2009
            • 29

            #6
            Thanks. Your help is much appreciated. Now, I understand why it's performing wrong. Thanks again.

            Comment

            Working...