Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading and only writing specific lines to a new texas file. 1

Status
Not open for further replies.

HoustonGuy

Programmer
Jun 29, 2000
165
US
I'm trying to get the logic right to read lines in a text file and only write certain lines to a new file. Once I find a specific line, I want to stop writing to the new file until another specific line is found.

The text file is like this:

A-Line A here
B-Line B here
C-Line C here
D-Line D here
E-Line E here
F-Line F here
G-Line G here
H-Line H here

I want the new file I wrote to to skip lines C thru G.
The new file would be:

A-Line A here
B-Line B here
H-Line H here

Although I've parsed plenty of times, I can't seem to get the logic correct to stop writing and pick up writing again to the text file.

Basic and stupid, so I appreciate the help.
 
Code:
        Dim lines_to_write(-1) As String
        For Each line As String In System.IO.File.ReadAllLines("C:\SomeFolder\SourceFile.txt")
            Dim FirstChar As Char = line.Substring(0, 1).ToUpper
            If Not (FirstChar = "C" Or FirstChar = "D" Or FirstChar = "E" Or FirstChar = "F" Or FirstChar = "G") Then
                ReDim Preserve lines_to_write(lines_to_write.Length)
                lines_to_write(lines_to_write.Length - 1) = line
            End If
        Next
        System.IO.File.WriteAllLines("C:\SomeFolder\TargetFile.txt", lines_to_write)
 
Thanks Riverguy!

The text example I gave wasn't entirely accurate, as I can now see.

The text is not that strictly formatted, so I need to be able to Stop writing at C and resume at H. The lines in between cannot be hard coded values as they may change.

I can easily stop writing when C is found but I'm stuck on resuming when H is found again.


 
Try this:
Code:
        Dim lines_to_write(-1) As String
	Dim WriteLine As Boolean = True
        For Each line As String In System.IO.File.ReadAllLines("C:\SomeFolder\SourceFile.txt")
            Dim FirstChar As Char = line.Substring(0, 1).ToUpper
            If FirstChar = "C" Then
                 WriteLine = False
            ElseIf FirstChar = "H" Then
                 WriteLine = True
            End If
            If WriteLine Then
                ReDim Preserve lines_to_write(lines_to_write.Length)
                lines_to_write(lines_to_write.Length - 1) = line
	    
            End If
        Next
        System.IO.File.WriteAllLines("C:\SomeFolder\TargetFile.txt", lines_to_write)
 
I'm almost there - but I still need to drop out of the evaluation until "H" = true.

Dim lines_to_write(-1) As String
Dim WriteLine As Boolean = True
For Each line As String In System.IO.File.ReadAllLines("C:\SomeFolder\SourceFile.txt")
Dim FirstChar As Char = line.Substring(0, 1).ToUpper

'--------------------------
If FirstChar = "C" Then
WriteLine = False <---writeline should equal false until FirstChar = "H"

'---------------------------
ElseIf FirstChar = "H" Then
WriteLine = True
End If
If WriteLine Then
ReDim Preserve lines_to_write(lines_to_write.Length)
lines_to_write(lines_to_write.Length - 1) = line

End If
Next
System.IO.File.WriteAllLines("C:\SomeFolder\TargetFile.txt", lines_to_write)


The results of this code removes "C" but keeps everything else.

I admit I've tried quite a few scenarios, but I can't find the correct logic to stop writing and then pick up again later. I've nested Do while's, Ifs, Do until's.

Haven't found it yet.
 
Try

Code:
       Dim lines_to_write(-1) As String
    Dim WriteLine As Boolean = True
        For Each line As String In System.IO.File.ReadAllLines("C:\SomeFolder\SourceFile.txt")
            Dim FirstChar As Char = line.Substring(0, 1).ToUpper

         Select Case FirstChar

            Case "A", "B", "H"
                WriteLine = True

            Case Else
                 WriteLine = False

         End Select

        Next
        System.IO.File.WriteAllLines("C:\SomeFolder\TargetFile.txt", lines_to_write)

I've not failed! Just found 100 ways that don't work...yet!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top