JackSparrow
JackSparrow

Reputation: 389

Editing text file?

I have a text file which has a lot of lines of information, what I am trying to do is search every line that begins with _ once I have found this line copy the line which is 6 lines below and add it to the 1st line below and delete the other rows of information.

For example:

_
Blue 
X
X
X
X
10.5
X
X
X
_
Orange 
X
X
X
X
15.1
X
X
X
_

I want to display as

Blue 10.5
Orange 15.1

I'm currently using VS 2010.

Upvotes: 0

Views: 60

Answers (1)

SysDragon
SysDragon

Reputation: 9888

Use IO.StreamReader to read the lines of the file:

Dim sr As New StreamReader(sFilePath)

And then loop the lines and do the things you asked:

Dim sLine, sResult As String
Dim iCont As Integer = -1

sResult = ""
sLine = sr.ReadLine()

While Not sr.EndOfStream
    If sLine.StartsWith("_"c) Then iCont = 0

    Select Case iCont
        Case 1
            sResult &= sLine & " "
        Case 6
            sResult &= sLine & Environment.NewLine()
    End Select

    If iCont >= 0 Then iCont += 1
    sLine = sr.ReadLine()
End While

sr.Close()
MessageBox.Show(sResult)

EDIT:
If you want to write it to a file then:

Dim sw As New StreamWriter(sNewFilePath)

sw.Write(sResult)
sw.Close()

If you didn't need anything else you can do this too:

IO.File.WriteAllText(sNewFilePath, sResult)


Documentation:

Upvotes: 2

Related Questions