473,395 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with Text file filtering

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!

Apr 24 '07 #1
2 1231
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()
Apr 24 '07 #2
***************
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

Quentin wrote:
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!
Apr 24 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Ayzin | last post by:
Hi, I have a problem that might be easy to solve(possibly, I've just overlooked an easy solution). Here we go: I have a dataset with 2 datatables in it. Now, I need to do the following: if...
16
by: nephish | last post by:
Hey there, kinda newbie question here. i know how to read the lines of a txt file. i know how to write a txt file. but how do i overwrite a line value with another value ? i mean, how do go...
0
by: CSDunn | last post by:
Hello, I have a problem with field filtering between an Access 2000 Project form (the application is called CELDT), and the report that shows the results of the filter. Both the form and the...
2
by: Mevar81 | last post by:
Hi to everybody.I have a problem with the PropertyGrid control.I want to display not all the properties of a generic Control(Button,TextBox,ComboBox,ecc.).In general I don't want to display only...
5
by: zfeld | last post by:
I am serializing an object to XML and writing it to disk. whenever a change to my object occurs I call the save function to re-write to disk (see code below). I am monitoring the directory where...
0
by: Tim::.. | last post by:
Hi... I'm having a big problem with a datagrid that obtains data from 2 different locations... Active Directory and SQL Database The data is inserted into a datatable and the sorted by a...
4
by: Joe | last post by:
I have a 'random quotes' plugin that I use which reads tab delimited quotes from multiple text files in a directory, and then randomly displays one. Each text file contains multiple lines, each...
0
by: Lyn | last post by:
I have a problem using the form .Filter and .FilterOn properties which causes Access to crash (as detailed in a separate post). The form operates in continuous mode, displaying matching records...
5
by: RHooper | last post by:
Hi, I'm new to Access, so I apologize if this question is trivial. I am trying to set-up a quick filter for users to define on a form bound to a table. I have a combo box called...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.