7 Replies - 27234 Views - Last Post: 17 February 2013 - 10:19 PM Rate Topic: -----

#1 mogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 10

Edit and Replace from Text File VB.NET

Posted 17 December 2010 - 03:31 PM

Hi,

Asking for support with locate and delete from text file.

Looking for unique term 'END' in file - want to delete the term as I will append text later in the routine and append a the same unique term 'END' later in the same routine.

Here another poster's (Thanks PsychoCoder) code to find the term:

Dim file As System.IO.File  
Dim reader as System.IO.StreamReader  
Dim line As String  
reader = file.OpenText("c:\mydoc.txt")  
'now loop through each line  
While reader.Peek <> -1  
line = reader.ReadLine()  
Now check for your specific word  
If line.Contains("END") Then  

       'Do your work here  
        MSGBOX("SUCCESS")
       'How do I delete -> line
    Else  

       'Do something else here 
        MSGBOX("TRY AGAIN")


    End If  

End While  

'close your reader  

reader.Close() 




I found that you can't convert easily a SteamWriter to String - which is how I thought this method SHOULD work.

The goal is to STRIP the line containing "END" - not replace it with a space and allow the routine to continue.

Please help?

Thanks!

~M

Is This A Good Question/Topic? 0
  • +

Replies To: Edit and Replace from Text File VB.NET

#2 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,138
  • Joined: 02-July 08

Re: Edit and Replace from Text File VB.NET

Posted 18 December 2010 - 06:07 AM

You need a collection to store and edit, then remake the file.

Dim lines As New List(Of String)
Using sr As New StreamReader("c:\mydoc.txt")
   While Not sr.EndOfStream
     lines.Add(sr.ReadLine)
   End While
End Using

For Each line As String In lines
  If line.Contains("End") Then
     lines.Remove(line)
     Exit For 'must exit as we changed the iteration 
  End If
Next

Using sw As New StreamWriter("c:\mydoc.txt")
   For Each line As String In lines
     sw.WriteLine(line)
   Next
End Using

Was This Post Helpful? 2
  • +
  • -

#3 mogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 10

Re: Edit and Replace from Text File VB.NET

Posted 20 December 2010 - 02:45 PM

View Posthawkvalley1, on 18 December 2010 - 08:07 AM, said:

You need a collection to store and edit, then remake the file.


Hello - just to say Thank you for an extremely helpful tip. This really does exactly what I described I needed for it to do!!

May I be so bold to ask a follow up question?

Can I do the same for

vbCrLf 'a carriage return



Thank you again for the great tip!

~M
Was This Post Helpful? 0
  • +
  • -

#4 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,138
  • Joined: 02-July 08

Re: Edit and Replace from Text File VB.NET

Posted 20 December 2010 - 02:50 PM

I prefer Environment.NewLine, but yes.
Was This Post Helpful? 0
  • +
  • -

#5 jalle007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-March 11

Re: Edit and Replace from Text File VB.NET

Posted 09 March 2011 - 05:47 AM

I have slightly change the code to allow multiple deletions.

Function FilterFile(ByVal sFile As String, ByVal sFilter As String) As Boolean

  Dim lines As New List(Of String)
  Try

   Using sr As New StreamReader(sFile)
    While Not sr.EndOfStream
     lines.Add(sr.ReadLine)
    End While
   End Using
   For i As Integer = lines.Count - 1 To 0 Step -1
    If lines(i).Contains(sFilter) Then lines.Remove(lines.Item(i))
   Next

   Using sw As New StreamWriter(sFile)
    For Each line As String In lines
     sw.WriteLine(line)
    Next
   End Using
   Return True
  Catch ex As Exception : Return False : End Try
 End Function


Hope it works
Was This Post Helpful? 0
  • +
  • -

#6 mogue  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 10

Re: Edit and Replace from Text File VB.NET

Posted 22 March 2011 - 09:52 AM

View Postjalle007, on 09 March 2011 - 08:47 AM, said:

I have slightly change the code to allow multiple deletions.

Function FilterFile(ByVal sFile As String, ByVal sFilter As String) As Boolean

  Dim lines As New List(Of String)
  Try

   Using sr As New StreamReader(sFile)
    While Not sr.EndOfStream
     lines.Add(sr.ReadLine)
    End While
   End Using
   For i As Integer = lines.Count - 1 To 0 Step -1
    If lines(i).Contains(sFilter) Then lines.Remove(lines.Item(i))
   Next

   Using sw As New StreamWriter(sFile)
    For Each line As String In lines
     sw.WriteLine(line)
    Next
   End Using
   Return True
  Catch ex As Exception : Return False : End Try
 End Function


Hope it works



Thanks for the response and change - I will get around to putting it into place and report back. Cheers! ~M
Was This Post Helpful? 0
  • +
  • -

#7 fzlan87  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-February 13

Re: Edit and Replace from Text File VB.NET

Posted 17 February 2013 - 09:36 PM

Hi, im using Visual Basic express 2010. I want to ask, how to edit/modify this program TO REPLACE LATEST FILES. I want the previous file to be replace with the new file. TQ

Public Class Form1
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

Dim createFile As String

createFile = TxtName.Text

My.Computer.FileSystem.WriteAllText("C:\Documents and Settings\My Documents\File1.txt", createFile, True)

MessageBox.Show("File Save", "Save")

End Sub
End Class
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is online

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,138
  • Joined: 02-July 08

Re: Edit and Replace from Text File VB.NET

Posted 17 February 2013 - 10:19 PM

Please make your own thread instead of digging up really old threads.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1