Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and replace text in a txt document using Visual Basic

Tags:

vb.net

Hi Im using Visual Basic 2008 Express Edition, my goal is to read an entire .txt file that works as a template and replace all ocurances of a word with a new one and save this new modified text in a new .txt when you press a command button. Can someone give me some tips?

like image 703
Felix Arturo Macias Ibarra Avatar asked Apr 12 '12 18:04

Felix Arturo Macias Ibarra


2 Answers

Dim fileReader As String = My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar")
My.Computer.FileSystem.WriteAllText("C:\test2.txt", fileReader, False)

So you should use the ReadAllText method of the FileSystem, pass the path as parameter and use the Replace method to replace what you want to replace. For more advanced usages of replace you can use regular expressions. You can read about that here.

Shorter version:

My.Computer.FileSystem.WriteAllText("C:\test2.txt", My.Computer.FileSystem.ReadAllText("C:\test.txt").Replace("foo", "bar"), False)
like image 55
Lajos Arpad Avatar answered Nov 13 '22 20:11

Lajos Arpad


I pulled this out of a a program I was making, just cut out the crap you don't need.

Private Sub UPDATECODES_Click(sender As Object, e As EventArgs) Handles UPDATECODES.Click
    Dim NEWCODE As String = NEWCODEBOX.Text
    Dim CC As String = CURRENTCODE.Text
    Dim oldcode As String
    Dim codelines(0 To 1) As String
    Dim olddate As String

    Using r1 As New System.IO.StreamReader(CODEDIR & "d.dat")
        olddate = r1.ReadToEnd
    End Using

    Using r2 As New System.IO.StreamReader(CODEDIR & "oc.dat")
        oldcode = r2.ReadToEnd
    End Using

    If System.IO.File.Exists(CODEDIR & "new code.txt") Then
        System.IO.File.Delete(CODEDIR & "new code.txt")
    End If

    If System.IO.File.Exists(CODEDIR & "CC.DAT") Then
        If IO.File.Exists(CODEDIR & "oc.dat") Then
            IO.File.Delete(CODEDIR & "OC.DAT")
        End If

        My.Computer.FileSystem.RenameFile(CODEDIR & "CC.DAT", "OC.DAT")
        Dim FILESTREAM As System.IO.FileStream
        FILESTREAM = New System.IO.FileStream(CODEDIR & "CC.DAT", System.IO.FileMode.Create)
        FILESTREAM.Close()
    End If

    Using WRITER As New System.IO.StreamWriter(CODEDIR & "CC.DAT")
        WRITER.WriteLine(NEWCODE)
    End Using

    Dim currentlines(0 To 1) As String
    Dim a As Integer
    Dim TextLine(0 To 1) As String
    a = 0

    Using sr As New System.IO.StreamReader(CODEDIR & "internet code.txt")
        While Not sr.EndOfStream
            ReDim codelines(0 To a)
            codelines(a) = sr.ReadLine()
            codelines(a) = codelines(a).Replace(CURRENTCODE.Text, NEWCODE)
            codelines(a) = codelines(a).Replace(olddate, DATEBOX1.Text & " - " & DATEBOX2.Text)
            Dim newfile As String = (CODEDIR & "new code.txt")
            Using sw As New System.IO.StreamWriter(newfile, True)
                sw.WriteLine(codelines(a))
            End Using
            a = a + 1
        End While
    End Using

    Using sw2 As New System.IO.StreamWriter(CODEDIR & "d.dat")
        sw2.WriteLine(date1 & " - " & date2)
    End Using

    System.IO.File.Delete(CODEDIR & "internet code.txt")
    My.Computer.FileSystem.RenameFile(CODEDIR & "new code.txt", "internet code.txt")
    CURRENTCODE.Text = NEWCODE
End Sub
like image 28
user2016683 Avatar answered Nov 13 '22 20:11

user2016683