Results 1 to 37 of 37

Thread: Help Needed on reading Text file lines

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Help Needed on reading Text file lines

    Hi All

    What i am trying to do is read lines 4 to 16 in a text file sample below for exsample line 5 has as a 0 then put this in chk(5) list box if line 5 is a 1 then put this in lst(5) listbox and so on. this it what i done so far.

    Dim intPos As Integer
    Dim intPos1 As Integer
    Dim intPos2 As Integer
    Dim strData5 As String
    Dim strData6 As String
    Dim strDummy As String
    Dim strNum As String
    Dim i As Integer
    intFile = FreeFile
    If Dir("D:\Output.txt") <> "" Then
    ' Timer2.Enabled = False
    Open "D:\Output.txt" For Input As intFile
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strData5
    Line Input #1, strData6

    intPos = InStr(strData6, "# P")
    intPos = InStr(strData5, "1")
    If intPos > 0 Then
    intPos1 = InStr(intPos + 3, strData6, " ")
    intPos2 = InStr(intPos + 1, strData5, " ")
    If intPos1 > 0 Then
    strNum = Mid$(strData6, intPos + 3, intPos1 - (intPos + 3))
    lst(CInt(strNum)).AddItem strData6
    If intPos2 > 0 Then
    strNum = Mid$(strData5, intPos + 2, intPos2 - (intPos + 2))
    chk(CInt(strNum)).AddItem strData5
    Close #intFile
    End If
    End If
    End If
    End If
    End Sub


    Text File Sample

    line 1 Check V0500
    line 2 # Check routine
    line 3 # PUMA EOL
    line 4 0
    line 5 1
    etc 1
    etc 1
    etc 1
    etc 0
    etc 0
    etc 0
    etc 0
    etc 1
    etc 1
    etc 0
    etc 0
    line 14 1
    line 15 1
    line 16 1
    line 17 # P2 PUMA EOL.CHR 29/11/2012 - 09:58:07
    fff


    regards
    steve

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Help Needed on reading Text file lines

    I still don't see what you're trying to achieve, but i can see some issues with this code

    Code:
    Open "D:\Output.txt" For Input As intFile
        Line Input #1, strDummy
        Line Input #1, strDummy
        Line Input #1, strDummy
        Line Input #1, strDummy
        Line Input #1, strData5
        Line Input #1, strData6
    You're not using intFile, instead you're hardcoding filenumber as #1. This will only work if FreeFile returns 1, if it returns something else it will not work


    Code:
    intPos = InStr(strData6, "# P")
        intPos = InStr(strData5, "1")
    You do realize that you're overwriting the result from the first statement in your second statement?

    As a general suggestion: If your setup of your textfile is always the same (e.g. always 17 lines, a string in line 1, an integer in Line 5 etc.) i would consider to redesign your code for using UDT's
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    Hi All

    I try and word it again. what I would like is if line 17 of the text file contains # P1 then if there is any 0's in the other lines
    sample below and to copy just the 0 lines to listboxs not the 1's i.e line 1 data to listbox1 and line 2 data to listbox2 and so on up to 16
    but just the 0 lines, but if line 17 contains # p2 then just copy the 0 again and not the 1 but to different named listboxs 1 to 16
    I want to do this with #p 3 and #p 4 to different named list boxes 1 to 16 as well maybe more in the future hope this explains better
    0
    0
    1
    0
    0
    0
    1
    1
    1
    1
    0
    1
    1
    0
    0
    # P1

    regards
    steve

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    Can you write this out in pseudo-code for us?

    e.g....

    if line 17 contains the string P1
    if lines 1 through 16 contain zeros
    ??????
    else
    do what?
    end if
    else
    do what here?
    endif

    The way you have explained it is very confusing......

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    LINE1 0 put this 0 in chkp1(1).additem
    LINE2 0 put this 0 in chkp1(2).additem
    LINE3 0 put this 0 in chkp1(3).additem
    LINE4 0 put this 0 in chkp1(4).additem
    LINE5 1 do nothing
    LINE6 0 put this 0 in chkp1(6).additem
    LINE7 0 put this 0 in chkp1(7).additem
    LINE8 0 put this 0 in chkp1(8).additem
    LINE9 1 do nothing
    LINE10 0 put this 0 in chkp1(10).additem
    LINE11 0 put this 0 in chkp1(11).additem
    LINE12 1 do nothing
    LINE13 0 put this 0 in chkp1(13).additem
    LINE14 0 put this 0 in chkp1(14).additem
    LINE15 0 put this 0 in chkp1(15).additem
    LINE16 0 put this 0 in chkp1(16).additem
    LINE 17 # P1 "P1"this identifiers to put the resualts in to chkp1 list boxes 1-16


    LINE1 0 put this 0 in chkp2(1).additem
    LINE2 1 do nothing
    LINE3 0 put this 0 in chkp2(3).additem
    LINE4 0 put this 0 in chkp2(4).additem
    LINE5 0 put this 0 in chkp2(5).additem
    LINE6 0 put this 0 in chkp2(6).additem
    LINE7 1 do nothing
    LINE8 0 put this 0 in chkp2(8).additem
    LINE9 0 put this 0 in chkp2(9).additem
    LINE10 0 put this 0 in chkp2(10).additem
    LINE11 0 put this 0 in chkp2(11).additem
    LINE12 0 put this 0 in chkp2(12).additem
    LINE13 0 put this 0 in chkp2(13).additem
    LINE14 0 put this 0 in chkp2(14).additem
    LINE15 0 put this 0 in chkp2(15).additem
    LINE16 0 put this 0 in chkp2(16).additem
    LINE 17 # P2 "P2" this identifiers to put the results in to chkp2 list boxes 1-16

    LINE1 0 put this 0 in chkp3(1).additem
    LINE2 0 put this 0 in chkp3(2).additem
    LINE3 0 put this 0 in chkp3(3).additem
    LINE4 0 put this 0 in chkp3(4).additem
    LINE5 0 put this 0 in chkp3(5).additem
    LINE6 0 put this 0 in chkp3(6).additem
    LINE7 0 put this 0 in chkp3(7).additem
    LINE8 0 put this 0 in chkp3(8).additem
    LINE9 0 put this 0 in chkp3(9).additem
    LINE10 0 put this 0 in chkp3(10).additem
    LINE11 0 put this 0 in chkp3(11).additem
    LINE12 0 put this 0 in chkp3(12).additem
    LINE13 0 put this 0 in chkp3(13).additem
    LINE14 0 put this 0 in chkp3(14).additem
    LINE15 0 put this 0 in chkp3(15).additem
    LINE16 1 do nothing
    LINE 17 # P3 "P3" this identifiers to put the results in to chkp3 list boxes 1-16

    LINE1 0 put this 0 in chkp4(1).additem
    LINE2 1 do nothing
    LINE3 0 put this 0 in chkp4(3).additem
    LINE4 0 put this 0 in chkp4(4).additem
    LINE5 0 put this 0 in chkp4(5).additem
    LINE6 0 put this 0 in chkp4(6).additem
    LINE7 0 put this 0 in chkp4(7).additem
    LINE8 0 put this 0 in chkp4(8).additem
    LINE9 0 put this 0 in chkp4(9).additem
    LINE10 0 put this 0 in chkp4(10).additem
    LINE11 0 put this 0 in chkp4(11).additem
    LINE12 0 put this 0 in chkp4(12).additem
    LINE13 0 put this 0 in chkp4(13).additem
    LINE14 0 put this 0 in chkp4(14).additem
    LINE15 0 put this 0 in chkp4(15).additem
    LINE16 0 put this 0 in chkp4(15).additem
    LINE 17 # P4 "P4" this identifiers to put the results in to chkp4 list boxes 1-16

    regards
    steve

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Help Needed on reading Text file lines

    So you do not know where to put the data until you have read the last line it seems.

    I would use an array
    Code:
    Dim X as Integer
    Dim FileLine(1 to 17) As String
    Open "D:\Output.txt" For Input As intFile
    Do While Not EOF
        X=X+1
        If x>17 then exit do
        Line Input #intFile, FileLine(x)
    Loop
    Close #intFile
    Now you would have all the lines in the array you can check FileLine(17) to see what you need to do and from there write a loop that checks each item in the array and puts the desired value in the desired location.

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: Help Needed on reading Text file lines

    DM, is it possible to use an array as a member of an UDT?
    Code:
    Private type UDTReadFile
    Descriptor as String
    MyValues(1 to 16) as integer 'or as boolean since it's ones and zeros
    End type
    If it's possible, he could read the whole block in one go, check the descriptor, and then assign the individual values to the correct listbox
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Help Needed on reading Text file lines

    very simple this should do it

    Code:
    Private Function ReadFile(FilePath As String)
    Dim i As Integer, FF As Integer, Num As Integer
    Dim s As String, ReadLine As String, sLine() As String
      
      FF = FreeFile
        Open FilePath For Input As #FF
          Do Until EOF(FF)
            Line Input #FF, ReadLine
            s = s & Trim(ReadLine) & ","
          Loop
        Close #FF
        
      sLine = Split(s, ",")
        Num = CInt(Right(sLine(16), Len(sLine(16)) - 3))
        Select Case Num
          Case 1
            For i = 0 To 15
              If sLine(i) = "0" Then chkp1(i + 1).AddItem sLine(i)
            Next
            
          Case 2
            For i = 0 To 15
              If sLine(i) = "0" Then chkp2(i + 1).AddItem sLine(i)
            Next
            
          Case 3
            For i = 0 To 15
              If sLine(i) = "0" Then chkp3(i + 1).AddItem sLine(i)
            Next
            
          Case 4
            For i = 0 To 15
              If sLine(i) = "0" Then chkp4(i + 1).AddItem sLine(i)
            Next
        End Select
    End Function
    Code:
    Private Sub Command1_Click()
      ReadFile "C:\File.txt"
    End Sub

    This was based on a file example like this
    Code:
    0
    0
    0
    1
    0
    0
    0
    1
    1
    1
    1
    0
    1
    1
    0
    0
    # P1 continue
    now if there was something written after the 1's and 0's you could modify a bit

    Example

    Code:
       Select Case Num
          Case 1
            For i = 0 To 15
              If Left(sLine(i), 1) = "0" Then chkp1(i + 1).AddItem Right(sLine(i), Len(sLine(i)) - 2)        Next
            '.....and change for every case 
        End Select
    Last edited by Max187Boucher; Dec 1st, 2012 at 12:58 AM.

  9. #9
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Help Needed on reading Text file lines

    I don't know what your User Interface looks like and whether it lends itself to using a Dynamic Control Array, but I'd use a single Control Array of ListBoxes rather than 4 different arrays.
    An example of what I mean:
    Code:
    Option Explicit
    
    Private Const MAX_ROWS As Long = 4  ' Number of Rows
    Private Const MAX_COLS As Long = 16 ' Number of Cols
    
    Private Sub chkp_Click(Index As Integer)
    Dim intRow As Integer
    Dim intCol As Integer
    intRow = (Index \ MAX_COLS) + 1
    intCol = (Index Mod MAX_COLS) + 1
    MsgBox "chkp" & CStr(intRow) & "(" & intCol & ") Clicked"
    End Sub
    
    Private Sub Command_Click()
    Dim intFile As Integer
    Dim intPos As Integer
    Dim intRow As Integer
    Dim intCol As Integer
    Dim intI As Integer
    Dim intJ As Integer
    Dim intStart As Integer
    Dim boInvalid As Boolean
    Dim strData As String
    Dim strLines() As String
    Dim strFile As String
    strFile = "C:\MyDir\MyData.txt" ' this is the file containing the values
    intFile = FreeFile()
    Open strFile For Input As intFile
    '
    ' Read the entire file and split into lines
    '
    strData = Input(LOF(intFile), intFile)
    Close intFile
    strLines = Split(strData, vbNewLine)
    Do
        '
        ' Look at the #P record and pick out the chkp 'number'
        '
        intPos = InStr(UCase(strLines(intI + MAX_COLS)), "P")
        If intPos > 0 Then
            '
            ' If it's in the right place then
            ' calculate which row this indicates
            '
            intRow = CInt(Mid$(strLines(intI + MAX_COLS), intPos + 1)) - 1
            '
            ' Process the 16 entries
            ' and add to the ListBox if the value is not '1'
            '
            For intJ = intI To intI + (MAX_COLS - 1)
                intCol = intStart
                If Trim$(strLines(intJ)) <> "1" Then
                    InsertValue intRow, intCol, strLines(intI)
                End If
                intI = intI + 1
                intStart = intStart + 1
            Next intJ
            intStart = 0
        Else
            boInvalid = True
        End If
        intI = intI + 1
    Loop Until intI >= UBound(strLines) Or boInvalid
    If boInvalid Then MsgBox "Data is out of Sequence - expected '#P' value"
    End Sub
    
    Private Sub Form_Load()
    '
    ' Assumes:  a ListBox named chkp with Index value of Zero
    '           a Label named lab with Index value of Zero
    '
    Dim intI As Integer
    Dim intLabW As Integer
    Dim lngTop As Long
    Dim lngLeft As Long
    '
    ' Put the Label somewhere
    ' and dimension appropriately
    '
    Lab(0).Top = 240
    Lab(0).Left = 680
    Lab(0).AutoSize = True
    Lab(0).Alignment = 2
    Lab(0).Caption = " chkp4(16) "
    Lab(0).AutoSize = False
    intLabW = Lab(0).Width
    Lab(0).Caption = "chkp1(1)"
    chkp(0).Top = Lab(0).Top + Lab(0).Height
    chkp(0).Left = 240
    chkp(0).Width = intLabW
    '
    ' Load the ListBoxes and Labels and format into Rows and Columns
    '
    For intI = 1 To MAX_ROWS * MAX_COLS - 1
        Load chkp(intI)
        Load Lab(intI)
        Lab(intI).Alignment = 2
        chkp(intI).Width = chkp(0).Width
        chkp(intI).Height = chkp(0).Height
        lngTop = chkp(0).Top + ((chkp(0).Height + Lab(0).Height) * (intI \ MAX_COLS))
        lngLeft = chkp(0).Left + (chkp(0).Width * (intI Mod MAX_COLS))
        chkp(intI).Left = lngLeft
        chkp(intI).Top = lngTop
        chkp(intI).Visible = True
        Lab(intI).Left = lngLeft
        Lab(intI).Top = lngTop - Lab(0).Height
        Lab(intI).Caption = "chkp" & CStr((intI \ MAX_COLS) + 1) & "(" & CStr((intI Mod MAX_COLS) + 1) & ")"
        Lab(intI).Visible = True
    Next intI
    Me.Width = ((MAX_COLS + 1) * Lab(0).Width) + 200
    End Sub
    
    Private Sub InsertValue(intRow As Integer, intCol As Integer, strData As String)
    Dim intIndex As Integer
    intIndex = (intRow * MAX_COLS) + intCol
    chkp(intIndex).AddItem strData
    End Sub
    I've attached the file I used to test it.
    Attached Files Attached Files
    Last edited by Doogle; Dec 1st, 2012 at 02:59 AM. Reason: Added the Click Event for completeness

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @MAX187BOUCHER
    Thanks for your code on this i need to start from line 5 - 20 there the 16 lines that contain 0's & 1's sorry for got to mention that, but on line 21 has the p1 to p4

    @DOOGLE
    like i mention above text # p1 is on line 25 and the 0's and 1's start from line 5
    regards
    steve
    Last edited by sbarber007; Dec 3rd, 2012 at 06:00 AM.

  11. #11
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Help Needed on reading Text file lines

    Just change the for i = 5 to 20 (might be 14 to 19 or 14 to 20) and... Even that is only 15 0's or 1's
    Andxhange Num = Cint...sLine(20)...
    Should be easy to fix

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @MAX187BOUCHER

    How can i change the lines below if there is something after # p3.chr 04/12/12 08:08 for example all i need is the number after the P but this number could be a double number in the future.

    Line Input #FF, ReadLine
    s = s & Trim(ReadLine) & ","
    Loop
    Close #FF

    sLine = Split(s, ",")
    Num = CInt(Right(sLine(16), Len(sLine(16)) - 3))

    regards
    steve

  13. #13
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Help Needed on reading Text file lines

    You could split the string Num

    Dim strSplit() As String
    '....
    Num = Right(sLine(16), Len(sLine(16)) - 3))
    strSplit = Split(Num, ".")
    Num = CInt(strSplit(0))


    Something like this... It is always good to try and explain this situation the first time because that way we dont have to modify code to try to suit your needs

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @Max187Boucher

    yea sorry about that i was sure there was nothing after # P1 but i was wrong i tryed change the code to what you wrote last but not having much success in this
    line Num = Right(sLine(16), Len(sLine(16)) - 3)) i put the cint( before right but still no good.

    regards

    steve

  15. #15
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Help Needed on reading Text file lines

    @steve: Can I suggest that you post an example file (i.e. exactly as it is) and also describe your full requirements. At the moment I get the feeling that you're 'drip feeding' us with what you want.

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    I am sorry for lack of information here is sample of text file below
    and the code below is how it stands now which is working if "# p03" is on its own on online 20 but has you can see
    from the text file it has ".chr 04/12/2012 - 17:47:23" in front of the "# P1" which cause a error I just need to use the number after the "p" to decide which chkpx listbox the 0's go in.

    Private Function ReadFile(FilePath As String)
    Dim i As Integer, FF As Integer, Num As Integer
    Dim s As String, ReadLine As String, sLine() As String

    FF = FreeFile
    Open FilePath For Input As #FF
    Do Until EOF(FF)
    Line Input #FF, ReadLine
    s = s & Trim(ReadLine) & ","
    Loop
    Close #FF

    sLine = Split(s, ",")
    Num = CInt(Right(sLine(19), Len(sLine(19)) - 3))
    Select Case Num
    Case 1
    For i = 3 To 18
    If sLine(i) = "0" Then chkp1(i - 2).AddItem sLine(i)
    Next

    Case 2
    For i = 3 To 18
    If sLine(i) = "0" Then chkp2(i - 2).AddItem sLine(i)
    Next

    Case 3
    For i = 3 To 18
    If sLine(i) = "0" Then chkp3(i - 2).AddItem sLine(i)
    Next

    Case 4
    For i = 3 To 18
    If sLine(i) = "0" Then chkp4(i - 2).AddItem sLine(i)
    Next
    End Select
    End Function

    Text file
    #
    #
    #
    0
    0
    0
    0
    1
    0
    1
    0
    1
    1
    1
    0
    1
    1
    0
    # P1.chr 04/12/2012 - 17:47:23

    Regards
    Steve

  17. #17
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    That should be sline 18, not 19.
    EDIT: that would be IF 'Text File' was not the first line, if it is, then 19 ids fine.

    In your example file, you want to use the '1' right after the P....? Then do this:

    Num = CInt(Mid(sLine(18), 4, 1))
    Last edited by SamOscarBrown; Dec 4th, 2012 at 03:21 PM.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @samOscarBrown

    Sorry I should explain little more the 1 after the P is not always a 1 it could be a 2,3 or 4 maybe more in future the text file can be and will be different each time this text file is created by another piece of software

    regards
    steve

  19. #19
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    Understand, but could it be larger than 9? That is, a double-digit int? The mid statement will return ONE character (the 4th one) in that array, no matter if it is a 1, 2, 3, 4, or up to 9. IF it IS just one character, will it always be in the fourth position. that is, will the P always be in the third position? If not, then use either split again, or instr to find the P and get the digit immediately following. You would have to know if the second character following the P was a numeric or not....if it is, then you could include it (and if you are going upwards into hundreds or thousands, you could keep checking, and and long as they are numeric, you can determine Num.) Hard to tell all your possibilities from above, but, the code works for me with the Mid statement....any variances is simply string manipulation to find a set of numeric characters which you then convert to an integer with CINT. If it is ever large numbers, you might have to go to CDBL.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @samoscarbrown

    Thanks for the info it should not be no more than 9 so are you saying that using
    Num = CInt(Mid(sLine(18), 4, 1)) will do # P1-9

    Regards
    Steve

  21. #21
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    yes sir.....as long as P is always the third character in that line and the number (1-9) immediately follows it.

    Let me know in your testing....working on mine.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @SamOscarBrown

    Thanks for that it worked great, but i used readline string before i.e "chkp1(0).additem readline" and that woud put the line data in the chkp1(0) listbox but not any more it seems like a blank character is going in there now. how would read that line 19 in to that listbox now. Thanks for your help

    Steve
    Last edited by sbarber007; Dec 5th, 2012 at 09:21 AM.

  23. #23
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    INternet has been down here last 24 hours.....will evaluate in a few mins and get back with you....

  24. #24
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    Well, what you have been doing in your code is not putting ANYTHING is chkp1(0)....you always start with chkp(1):

    For i = 3 To 18
    If sLine(i) = "0" Then chkp1(i - 2).AddItem sLine(i)
    Next

    see? If i = 3, then chkp1(i-2) would be chkp(1), not chkp(0).

    change it to:

    For i = 3 To 18
    If sLine(i) = "0" Then chkp1(i - 3).AddItem sLine(i)
    Next

  25. #25
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Help Needed on reading Text file lines

    sorry i havent got back to you but seems like sam is doing a good job... its just a matter of having the right numbers at the right moment

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    Thanks for all your help
    Just one more question on this matter using the Sline(i) string i want to overwrite a text file but the code below keeps adding to it how do i change this code for overwrighting it.

    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i)
    Close #3
    it needs to write like this.
    0
    1
    1
    1
    1
    1
    1
    1
    1
    not in a row "1111111111111111111" when i use f.write it does it like this.

    regards
    Steve

  27. #27
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    in your loop (I'm assuming you are looping through sline(i), put a VBCR (Visual Basic Carriage Return) in the loop after sline(i),

    e.g,
    for i = 0 to ubound (sline()) -1
    print # 3, sline(i) & vbCr
    next i

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @samOscarBrown
    the problem i got is


    Private Function ReadFile(FilePath As String)
    Dim i As Integer, FF As Integer, Num As Integer
    Dim s As String, ReadLine As String, sLine() As String
    Dim sum As String
    Dim strData As String
    FF = FreeFile
    Open FilePath For Input As #FF
    Do Until EOF(FF)
    Line Input #FF, ReadLine
    s = s & Trim(ReadLine) & ","
    Loop
    Close #FF

    sLine = Split(s, ",")
    Num = CInt(Mid(sLine(19), 4, 1))

    Select Case Num
    Case 1
    For i = 3 To 18
    If sLine(i) = "0" Then CHKP1(i - 2).AddItem sLine(i)
    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i) this here gives me the 0's 1's in a text file but i would like this work further down the code

    Close #3


    Next

    CHKP1(0).AddItem sLine(i) this here gives me the # p1 number which is ok
    LABP11.Caption = CHKP1(1).ListCount
    LABP12.Caption = CHKP1(2).ListCount
    LABP13.Caption = CHKP1(3).ListCount
    LABP14.Caption = CHKP1(4).ListCount
    LABP15.Caption = CHKP1(5).ListCount
    LABP16.Caption = CHKP1(6).ListCount
    LABP17.Caption = CHKP1(7).ListCount
    LABP18.Caption = CHKP1(8).ListCount
    LABP19.Caption = CHKP1(9).ListCount
    LABP1A.Caption = CHKP1(10).ListCount
    LABP1B.Caption = CHKP1(11).ListCount
    LABP1C.Caption = CHKP1(12).ListCount
    LABP1D.Caption = CHKP1(13).ListCount
    LABP1E.Caption = CHKP1(14).ListCount
    LABP1F.Caption = CHKP1(15).ListCount
    LABP1G.Caption = CHKP1(16).ListCount
    Label4.Caption = CHKP1(0).ListCount

    sum = Val(LABP11.Caption) + Val(LABP12.Caption) + Val(LABP13.Caption) + Val(LABP14.Caption) + Val(LABP15.Caption) + Val(LABP16.Caption) + Val(LABP17.Caption) + Val(LABP18.Caption) + Val(LABP19.Caption) + Val(LABP1A.Caption) + Val(LABP1B.Caption) + Val(LABP1C.Caption) + Val(LABP1D.Caption) + Val(LABP1E.Caption) + Val(LABP1F.Caption) + Val(LABP1G.Caption)
    Label3.Caption = sum
    sum = Val(Label3.Caption) + Val(Label5.Caption) + Val(Label7.Caption) + Val(Label10.Caption)
    NOK.Caption = sum
    sum = Val(Label4.Caption) + Val(Label6.Caption) + Val(Label8.Caption) + Val(Label11.Caption)
    POK.Caption = sum


    Case 2
    For i = 3 To 18
    If sLine(i) = "0" Then CHKP2(i - 2).AddItem sLine(i)

    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i)
    Close #3 Next

    CHKP2(0).AddItem sLine(i) this here gives me the # p2 number which is ok
    LABP21.Caption = CHKP2(1).ListCount
    LABP22.Caption = CHKP2(2).ListCount
    LABP23.Caption = CHKP2(3).ListCount
    LABP24.Caption = CHKP2(4).ListCount
    LABP25.Caption = CHKP2(5).ListCount
    LABP26.Caption = CHKP2(6).ListCount
    LABP27.Caption = CHKP2(7).ListCount
    LABP28.Caption = CHKP2(8).ListCount
    LABP29.Caption = CHKP2(9).ListCount
    LABP2A.Caption = CHKP2(10).ListCount
    LABP2B.Caption = CHKP2(11).ListCount
    LABP2C.Caption = CHKP2(12).ListCount
    LABP2D.Caption = CHKP2(13).ListCount
    LABP2E.Caption = CHKP2(14).ListCount
    LABP2F.Caption = CHKP2(15).ListCount
    LABP2G.Caption = CHKP2(16).ListCount
    Label6.Caption = CHKP2(0).ListCount

    sum = Val(LABP21.Caption) + Val(LABP22.Caption) + Val(LABP23.Caption) + Val(LABP24.Caption) + Val(LABP25.Caption) + Val(LABP26.Caption) + Val(LABP27.Caption) + Val(LABP28.Caption) + Val(LABP29.Caption) + Val(LABP2A.Caption) + Val(LABP2B.Caption) + Val(LABP2C.Caption) + Val(LABP2D.Caption) + Val(LABP2E.Caption) + Val(LABP2F.Caption) + Val(LABP2G.Caption)
    Label5.Caption = sum
    sum = Val(Label3.Caption) + Val(Label5.Caption) + Val(Label7.Caption) + Val(Label10.Caption)
    NOK.Caption = sum
    sum = Val(Label4.Caption) + Val(Label6.Caption) + Val(Label8.Caption) + Val(Label11.Caption)
    POK.Caption = sum


    Case 3
    For i = 3 To 18
    If sLine(i) = "0" Then CHKP3(i - 2).AddItem sLine(i)
    Kill "d:" & strRX & "\check.txt"
    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i)
    Close #3
    Next

    CHKP3(0).AddItem sLine(i) this here gives me the # p3 number which is ok
    LABP31.Caption = CHKP3(1).ListCount
    LABP32.Caption = CHKP3(2).ListCount
    LABP33.Caption = CHKP3(3).ListCount
    LABP34.Caption = CHKP3(4).ListCount
    LABP35.Caption = CHKP3(5).ListCount
    LABP36.Caption = CHKP3(6).ListCount
    LABP37.Caption = CHKP3(7).ListCount
    LABP38.Caption = CHKP3(8).ListCount
    LABP39.Caption = CHKP3(9).ListCount
    LABP3A.Caption = CHKP3(10).ListCount
    LABP3B.Caption = CHKP3(11).ListCount
    LABP3C.Caption = CHKP3(12).ListCount
    LABP3D.Caption = CHKP3(13).ListCount
    LABP3E.Caption = CHKP3(14).ListCount
    LABP3F.Caption = CHKP3(15).ListCount
    LABP3G.Caption = CHKP3(16).ListCount
    Label8.Caption = CHKP3(0).ListCount

    sum = Val(LABP31.Caption) + Val(LABP32.Caption) + Val(LABP33.Caption) + Val(LABP34.Caption) + Val(LABP35.Caption) + Val(LABP36.Caption) + Val(LABP37.Caption) + Val(LABP38.Caption) + Val(LABP39.Caption) + Val(LABP3A.Caption) + Val(LABP3B.Caption) + Val(LABP3C.Caption) + Val(LABP3D.Caption) + Val(LABP3E.Caption) + Val(LABP3F.Caption) + Val(LABP3G.Caption)
    Label7.Caption = sum
    sum = Val(Label3.Caption) + Val(Label5.Caption) + Val(Label7.Caption) + Val(Label10.Caption)
    NOK.Caption = sum
    sum = Val(Label4.Caption) + Val(Label6.Caption) + Val(Label8.Caption) + Val(Label11.Caption)
    POK.Caption = sum

    Case 4
    For i = 3 To 18
    If sLine(i) = "0" Then CHKP4(i - 2).AddItem sLine(i)

    Open "D:\" & strRX & "\check.txt" For Append As #2
    Print #2, sLine(i)
    Close #2
    Next

    CHKP4(0).AddItem sLine(i) this here gives me the # p4 number which is ok
    LABP41.Caption = CHKP4(1).ListCount
    LABP42.Caption = CHKP4(2).ListCount
    LABP43.Caption = CHKP4(3).ListCount
    LABP44.Caption = CHKP4(4).ListCount
    LABP45.Caption = CHKP4(5).ListCount
    LABP46.Caption = CHKP4(6).ListCount
    LABP47.Caption = CHKP4(7).ListCount
    LABP48.Caption = CHKP4(8).ListCount
    LABP49.Caption = CHKP4(9).ListCount
    LABP4A.Caption = CHKP4(10).ListCount
    LABP4B.Caption = CHKP4(11).ListCount
    LABP4C.Caption = CHKP4(12).ListCount
    LABP4D.Caption = CHKP4(13).ListCount
    LABP4E.Caption = CHKP4(14).ListCount
    LABP4F.Caption = CHKP4(15).ListCount
    LABP4G.Caption = CHKP4(16).ListCount
    Label11.Caption = CHKP4(0).ListCount

    sum = Val(LABP41.Caption) + Val(LABP42.Caption) + Val(LABP43.Caption) + Val(LABP44.Caption) + Val(LABP45.Caption) + Val(LABP46.Caption) + Val(LABP47.Caption) + Val(LABP48.Caption) + Val(LABP49.Caption) + Val(LABP4A.Caption) + Val(LABP4B.Caption) + Val(LABP4C.Caption) + Val(LABP4D.Caption) + Val(LABP4E.Caption) + Val(LABP4F.Caption) + Val(LABP4G.Caption)
    Label10.Caption = sum
    sum = Val(Label3.Caption) + Val(Label5.Caption) + Val(Label7.Caption) + Val(Label10.Caption)
    NOK.Caption = sum
    sum = Val(Label4.Caption) + Val(Label6.Caption) + Val(Label8.Caption) + Val(Label11.Caption)
    POK.Caption = sum
    Case 5
    For i = 0 To UBound(sLine()) - 1
    Open "D:\7010201262094\check.txt" For Append As #3

    Print #3, sLine(i) & vbCr
    Next i
    Close #3
    End Select


    Dim intFile As Integer
    Dim strDummy As String
    Dim strData21 As String
    intFile = FreeFile
    If Dir("D:\output.txt") <> "" Then
    ' Timer2.Enabled = False
    Open "D:\output.txt" For Input As intFile
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strDummy
    Line Input #1, strData21
    Close #intFile


    End If

    If strData21 = "fff" Then
    Open "D:\" & strRX & "\check.txt" For Append As #2
    Print #2, sLine(i) this is where i would like to save the 0's 1's in a text file and overwright the text file
    Close #2
    Open "D:\REJECTS.txt" For Append As #1
    Print #1, strRX & " REJECTED ON " & sLine(i)
    Close #1

    Open "D:\" & strRX & "\REJECTS.txt" For Append As #2
    Print #2, strRX & " REJECTED ON " & sLine(i)
    Close #2
    ViewRejects.ViewList.AddItem strRX & " REJECTED ON " & sLine(i)and for this line to give me # p1 which it does



    End If
    End Function

    Sorry to be a pain

    regards
    steve
    Last edited by sbarber007; Dec 6th, 2012 at 03:41 PM.

  29. #29
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    SO, sline(0) might be 1 or 0, as might sline(1), or sline(2), etc....correct?
    If you simply loop through the sline array (say, 1 to 18), and send that data to textbox, you would get "100010010"...etc.
    like: for i = 1 to 18
    text1.text = text1.text + sline(1)
    next x
    but if you did this, you would could put that info into a listbox, a multiline textbox OR the file you are creating....
    for i = 1 to 18
    list1.additem(sline(1) + vbCR)
    ...or print to your file
    next x

    it SHOULD appear as

    1
    0
    0
    0
    1
    0
    0
    etc

    I have NOT tried it with your code, but it is a simple set of strings and should work just fine....(unless I made some syntax error).

  30. #30
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Help Needed on reading Text file lines

    Quote Originally Posted by sbarber007 View Post
    Thanks for all your help
    Just one more question on this matter using the Sline(i) string i want to overwrite a text file but the code below keeps adding to it how do i change this code for overwrighting it.

    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i)
    Close #3
    it needs to write like this.
    0
    1
    1
    1
    1
    1
    1
    1
    1
    not in a row "1111111111111111111" when i use f.write it does it like this.

    regards
    Steve
    I can't tell if you are saying that you want it all on one line or not

    Quote Originally Posted by SamOscarBrown View Post
    in your loop (I'm assuming you are looping through sline(i), put a VBCR (Visual Basic Carriage Return) in the loop after sline(i),

    e.g,
    for i = 0 to ubound (sline()) -1
    print # 3, sline(i) & vbCr
    next i
    That would actually add a blank line between each value

    Print # prints a CRLF at the end by default, adding one would cause 2 of them to be written resulting in a blank line between enteries.

    when using Print # you can add a ; on the end to surpress the CRLF

    Code:
    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i)
    Close #3
    ^^ Would print each value on a seperate line

    Code:
    Open "D:\" & strRX & "\check.txt" For Append As #3
    Print #3, sLine(i) ;
    Close #3
    ^^ Would print each value on the same line

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @Datamiser
    I dont want it on 1 line each line to have crlf so using Print #3, sLine(i) but i want it overwrite the text file each time not adding to it

    @SamOscarBrown
    Thanks think i worked it out now with your help thanks again the only prob i still got is overwrite the text file each time not adding to it
    Last edited by sbarber007; Dec 6th, 2012 at 04:57 PM.

  32. #32
    PowerPoster ThEiMp's Avatar
    Join Date
    Dec 2007
    Location
    Take The PCI Bus Across To The CPU!!
    Posts
    3,899

    Re: Help Needed on reading Text file lines

    You can actually cheat by doing this:
    Copy the Data into a RichTextBox and then grab which line you need by using the SelText property for the RichTextBox...
    I have a huge free products range, of computer software in which you can download using any kind of 64-Bit Web Browser. Also there is coming a Social Networking section that I am making on my Website...

    |Ambra Productions Inc. | The Black Sun Society | The Black Shield | Ambra College | Church of the Black Sun | Ambra Productions Inc's Homepage | Boomtick Event's Venues: Ambar Nightclub, Jack Rabbit Slim's, Villa Nightclub and Lucy's Bar | Pasta Ambra | Fish Feast Company | Wallet Wizard | Ambrose Liquor | Ambar Tavern | Ambra University |

    Do you wish to do unpaid work for me??? If so, the PM me on this Forum, and then we can get to work, programming for the future of computers go by the name of ThEiMp. This is my ghost writers name. Also my nickname, means that I am: The Imperial of the Technology Industry, so then to make it really short, I just then wrote: The Imp, which is where I get the nickname from...

  33. #33
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Help Needed on reading Text file lines

    You said earlier you did not want to add to a file but you are using Append which you would have to use Output instead so it overwrites the text in The file

  34. #34
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    As Max pointed out...don't use append. Should take care of it......good luck...let us know.

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2012
    Location
    Essex
    Posts
    273

    Re: Help Needed on reading Text file lines

    @SamOscarBrown & @ Max187Boucher using code below put only getting 1 character in the text file not 16

    for i=3 to 18
    Open "D:\" & strRX & "\check.txt" For Output As #3
    Print #3, sLine(i)
    Close #3

  36. #36
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,143

    Re: Help Needed on reading Text file lines

    OPen the file before the loop

  37. #37
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: Help Needed on reading Text file lines

    sBarber, as i said before you need to explain in the first place what you want to acheive! Now i'm kinda lost about what you want i took exactly your words from your first posts and then did a good example for you, but you didnt tell us the right way to do it.

    Now we're kinda confused, even Doogle tried to help you but after you changed what yoh wanted. The best way for us to help you would be to havea file example(s) of the same file(s) you are using please post the text file(s)... At least one then you will see the light at the end of the tunnel.

    Sam pointed out you need to open file before loop... In my example i opened file before loop or else yes it will only get one line as you are opening file every for-next loop you are using

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