Below is a sample of my text:

01"111111","2345678","7891090","M0000023"
02"111111","2345678","7891090","M0000023"
03"111111","2345678","7891090","M0000023"
01"111111","2345678",
"7891090","M0000023"
02"111111","2345678",7891090","M0000023"
03"111111","2345678",7891090","M0000023"
etc.

Sometime the input file has lines that wrapped and sometimes it does not. I need to read the text file line by line and then if the line does not begin with "01" or "02" or "03" join it to the line above it and write the fixed file to a new text file.

Imports System.IO

Public Class Form1

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim LineReader As StreamReader

        Dim results As DialogResult
        


        results = OpenFileDialog1.ShowDialog

        If results = DialogResult.OK Then

            LineReader = New StreamReader(OpenFileDialog1.FileName)

            TextBox1.Text = LineReader.ReadLine()

            LineReader.Close()

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim FileWriter As StreamWriter

        Dim results As DialogResult

        results = SaveFileDialog1.ShowDialog

        If results = DialogResult.OK Then

            FileWriter = New StreamWriter(SaveFileDialog1.FileName, False)

            FileWriter.Write(TextBox1.Text)

            FileWriter.Close()

        End If
    End Sub

    End Class

How can I do this with the code I have so far?

Thanks.

>How can I do this with the code I have so far?

Think about ReadAllLines() method.

.....
        Dim s() As String = System.IO.File.ReadAllLines("file1.txt")
        Dim i As Integer
        For i = 0 To s.Length - 1
            If Not (s(i).StartsWith("01") Or s(i).StartsWith("02") Or s(i).StartsWith("03")) Then
                s(i - 1) = s(i - 1) & s(i)
                s(i) = ""
            End If
        Next
     ....
Comments
you stole my answer

This is what I ended up with and seems to work. Can I get help with getting it to read to the EOF? I don't seem to be able to get the file number thing down. EOF()

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s() As String = System.IO.File.ReadAllLines("g:\users\all users\sorting room info\lmc reports\test.txt")
        Dim i As Integer

        For i = 0 To s.Length - 1
            If Not (s(i).StartsWith("01") Or (s(i).StartsWith("02") Or (s(i).StartsWith("03")))) Then
                TextBox1.Text = s(i - 1) & s(i - 0) & s(i) _
                & vbCrLf & s(i + 1) & vbCrLf _
                & s(i + 2)
            End If
        Next
    End Sub

Below is a sample of my text:

01"111111","2345678","7891090","M0000023"
02"111111","2345678","7891090","M0000023"
03"111111","2345678","7891090","M0000023"
01"111111","2345678",
"7891090","M0000023"
02"111111","2345678",7891090","M0000023"
03"111111","2345678",7891090","M0000023"
etc.

Sometime the input file has lines that wrapped and sometimes it does not. I need to read the text file line by line and then if the line does not begin with "01" or "02" or "03" join it to the line above it and write the fixed file to a new text file.

Imports System.IO

Public Class Form1

    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim LineReader As StreamReader

        Dim results As DialogResult
        


        results = OpenFileDialog1.ShowDialog

        If results = DialogResult.OK Then

            LineReader = New StreamReader(OpenFileDialog1.FileName)

            TextBox1.Text = LineReader.ReadLine()

            LineReader.Close()

        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim FileWriter As StreamWriter

        Dim results As DialogResult

        results = SaveFileDialog1.ShowDialog

        If results = DialogResult.OK Then

            FileWriter = New StreamWriter(SaveFileDialog1.FileName, False)

            FileWriter.Write(TextBox1.Text)

            FileWriter.Close()

        End If
    End Sub

    End Class

How can I do this with the code I have so far?

Thanks.

Edited 6 Years Ago by Nick Evan: fixed tags

This article has been dead for over six months. Start a new discussion instead.