I am currently working with a text file and cannot seem to get it to
filter right. I am trying to delete every line above a keyword
"Recipe" and every line below a keyword. Or just move every line
between those key words to a new file. Either way I having
difficulties. Below is a sample of the text file and code.
Sample file (only original is much larger)
2,3,56,554,22,11,1,2,9,
234,42,12,4,4,4,,2,3,2,3
"Recipe"
2,3,32545,655,5656,56,
4,56,4565,56,565,654,45,
2,45,46,456,473,6,67565
"End of Recipe"
2,7,5,7,452,7,6,8,5,4,7,4
54,6,6,4,6,578,5,6,56,7,5,
I would only like to keep what is inbetween Recipe and End of Recipe.
Dim streamR As StreamReader = IO.File.OpenText_("c:\MultiTemp1.txt")
Dim streamw As StreamWriter = IO.File.CreateText_("c:
\filtered2.txt")
Dim strInput As String
While Not streamR.EndOfStream
strInput = streamR.ReadLine
If strInput.Contains("End") Then
Else
If InStr(strInput, "Recipe", CompareMethod.Text) > 0
Then
strInput = streamR.ReadLine
streamw.WriteLine(strInput)
End If
End If
End While
streamw.Close()
streamR.Close()
Any help would by great.thanks!
Duracel - 24 Apr 2007 15:29 GMT
Well the easiest way is to set a flag "bWriting" when you start writing and
clear the flag when you stop (i.e. when you encounter your "End"). Of
course this won't work if the "End" comes directly after the "Recipie",
because the output file will contain the "End" line as well. I leave that
as an excersise for you just in case this is school/coursework (its a simple
fix) ;). I didn't test the code below, so.....
Dim streamR As StreamReader = IO.File.OpenText("c:\MultiTemp1.txt")
Dim streamw As StreamWriter = IO.File.CreateText("c:\filtered2.txt")
Dim strInput As String
Dim bWriting As Boolean = False
While Not streamR.EndOfStream
strInput = streamR.ReadLine
If strInput.StartsWith("""Recipie") Then
bWriting = True
strInput = streamR.ReadLine
ElseIf strInput.StartsWith("""End") Then
bWriting = False
strInput = streamR.ReadLine
End If
If bWriting Then
streamw.WriteLine(strInput)
End If
End While
streamw.Close()
streamR.Close()
CoRrRan - 24 Apr 2007 15:34 GMT
***************
Imports System
Imports System.IO
Imports System.IO.FileStream
Public Class Form1
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim streamR As StreamReader = New StreamReader("C:\Temp\Input.txt")
Dim streamW As StreamWriter = New StreamWriter("C:\Temp\Output.txt")
Dim strInput As String
While Not streamR.EndOfStream
strInput = streamR.ReadLine
If strInput.Contains("Recipe") Then
strInput = streamR.ReadLine
While Not strInput.Contains("End of Recipe")
streamW.WriteLine(strInput)
strInput = streamR.ReadLine
End While
streamW.Flush()
streamW.Close()
End If
End While
streamR.Close()
End Sub
End Class
*************************
Assumptions: UserForm is called "Form1", 1 button on UserForm called
"Button1".
HTH, CoRrRan
> I am currently working with a text file and cannot seem to get it to
> filter right. I am trying to delete every line above a keyword
[quoted text clipped - 41 lines]
>
> Any help would by great.thanks!