Results 1 to 4 of 4

Thread: [RESOLVED] Remove line from text file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Resolved [RESOLVED] Remove line from text file

    I have text file that need to read until *+*+* Top *+*+*
    and write the content until *+*+* Top *+*+* but i dont want to write *+*+* Top *+*+* this line

    This is my code so far

    Code:
     Dim myReader As StreamReader = New StreamReader("C:\temp2.txt")
            Dim myLine As String
                   Dim fc As StreamWriter = File.CreateText("C:\kaya1.txt")
    
    
            Dim sf As StreamWriter = File.CreateText("C:\kaya.txt")
    
    
            Do
                myLine = myReader.ReadLine()
                fc.WriteLine(myLine)
    If myLine.Contains("*+*+* Top *+*+*") Then
                    myLine.Replace("*+*+* Top *+*+*", "")
                End If
            Loop Until (myLine.Contains("*+*+* Top *+*+*"))
    
            sf.Write(myReader.ReadToEnd())
    
    
            myReader.Close()
            myReader = Nothing
            sf.Close()
            fc.Close()
    When i use Replace function to replace that content with space, it still write that line. And I dont know how to fix this problem?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Remove line from text file

    Have a look at your code and ask yourself what it actually does:
    Code:
    myLine = myReader.ReadLine()
    fc.WriteLine(myLine)
    
    If myLine.Contains("*+*+* Top *+*+*") Then
        myLine.Replace("*+*+* Top *+*+*", "")
    End If
    It reads the line from one file, then writes it to another, then tests whether it contains the text, then replaces the text. What's the point of testing the line for that text and replacing it if you've already written it to the second file? Now, ask yourself what you actually want to happen. You want to read the line, test if it contains the text and then only write it to the second file If it does Not contain the text.

    When you actually write down what you want to happen, isn't it then easier to write code to do it? If you don't even know what steps your code is supposed to be executing then what chance it will execute those steps?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Re: Remove line from text file

    Hey kayatri... You could read each line, check the text and then write it to another file. If it's the string to you want it to stop at, then it should exit the writer. Another way would be to store it into an array and check, then copy.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    243

    Re: Remove line from text file

    thanx tassa and jimchilhinney i solved it with ur help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width