Results 1 to 16 of 16

Thread: Delete specified line in text file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    67

    Delete specified line in text file

    Another question:

    How to delete specified line in text file?

    Thanks,

    David

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Delete specified line in text file

    You'll need to read the whole file in, and then write it back out (with the exception of the line you don't want in the file).
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    67

    Re: Delete specified line in text file

    Any other solution? I don't like this

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Delete specified line in text file

    Quote Originally Posted by manowar
    Any other solution? I don't like this
    You could always give up and move on to another project.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    67

    Re: Delete specified line in text file

    Ok.

    Than tell me this. If I load whole text from file. How can I than delete those line ?

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Delete specified line in text file

    Something like this should do...
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) _
                                  Handles Button1.Click
            Dim sr As StreamReader
            Dim sw As StreamWriter
            Dim arrLines As New ArrayList
    
            sr = New StreamReader(New FileStream("c:\somedoc.txt", FileMode.Open))
    
            While sr.Peek <> (-1)
                Dim s As String = sr.ReadLine
    
                If s <> SomeCriteriaToDetermineThatItShouldBeDeleted() Then
                    arrLines.Add(s)
                End If
            End While
    
            sr.Close()
    
            sw = New StreamWriter(New FileStream("c:\somedoc.txt", FileMode.Create))
    
            For Each s As String In arrLines
                sw.WriteLine(s)
            Next
    
            sw.Close()
    
        End Sub
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    67

    Re: Delete specified line in text file

    If is there any other solution please let me know please.

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

    Re: Delete specified line in text file

    What type of data is in the text file? If it is something that could be stored in an XML file then that might be a better option. .NET provides methods to deal with XML in an OO fashion. Text is just one long string and it's up to you to find the bit you don't want and get rid of it.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    67

    Re: Delete specified line in text file

    it normal txt file with text.

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

    Re: Delete specified line in text file

    Quote Originally Posted by manowar
    it normal txt file with text.
    But what does it represent? Is it just a series of words, like a story or something, or does it have a well defined structure? If it is structured then XML may be a possibility. Otherwise, it's read in, find, delete, resave.

  11. #11
    Hyperactive Member jonwondering's Avatar
    Join Date
    May 2005
    Posts
    311

    Re: Delete specified line in text file

    if you use the find, delete, resave procedure then you might even want to consider the replace() function - might be a little simpler.

    Read in the file into one string (i am assuming it's not big - watch out for overflow)
    Replace(String, NotNeededStr, ReplaceWithNothing)
    Delete file
    Write back the whole string to the file

  12. #12
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Delete specified line in text file

    Why dont you like Crptcblades code example? It does what you asked.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  13. #13
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Delete specified line in text file

    Quote Originally Posted by RobDog888
    Why dont you like Crptcblades code example? It does what you asked.
    They don't like me 'cause I'm black.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  14. #14
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Delete specified line in text file

    I thought you were an evil space elf with those pointy ears and yellow Don King hair?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  15. #15
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Delete specified line in text file

    Only on my mother's side
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  16. #16
    Junior Member
    Join Date
    Jan 2008
    Posts
    19

    Re: Delete specified line in text file

    crpt's code, nicely done =]

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