Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / April 2007

Tip: Looking for answers? Try searching our database.

Problem with Text file filtering

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Quentin - 24 Apr 2007 14:57 GMT
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!

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.