Search code examples
loopsprintingvb6

How to print every second line of the text in .txt file using vb 6


I have a program that reads all info from .txt file and after this showing in MsgBox. Need to know, how to print every second line (every second name) from this file and show in MsgBox. Here is my code:

Sub Printed
Dim sFileText As String
Dim sFinal as String
Dim iFileNo As Integer
iFileNo = FreeFile
Open "D:\Information\data.txt" For Input As #iFileNo
Do While Not EOF(iFileNo)
  Input #iFileNo, sFileText
sFinal = sFinal & sFileText & vbCRLF
Loop
MsgBox sFinal
Close #iFileNo

End Sub

Here is, how .txt file looks

Here is, how .txt file looks

Here is how it showing now in MsgBox: printed info now


Solution

  • Try this:

    Sub Printed
        Dim sFileText As String
        Dim sFinal as String
        Dim iFileNo As Integer
        Dim fIgnoreLine As Boolean
        iFileNo = FreeFile
        
        ' fIgnoreLine = True    ' uncomment this line if you want to print every even line
        Open "D:\Information\data.txt" For Input As #iFileNo
        Do While Not EOF(iFileNo)
            Line Input #iFileNo, sFileText
            if Not fIgnoreLine then sFinal = sFinal & sFileText & vbCRLF
            fIgnoreLine = Not fIgnoreLine
        Loop
        MsgBox sFinal
        Close #iFileNo
    
    End Sub