6

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?

4 Answers 4

10
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)
7
  • Thanks I copy pasted your code in the command button click event and when I build the program, when I click the button there is no replacing done on the text file it still filled with foo. Apr 12, 2012 at 19:25
  • I see. The String fileReader contains the correct text right? Your problem is that the file doesn't refresh. My code was made only for reading the text, writing was not done yet, but I'll update my code soon. Apr 12, 2012 at 19:31
  • Ok I added My.Computer.FileSystem.WriteAllText("C:\Users\EMACFEL\Documents\test.txt", fileReader, False) and it worked thanks Apr 12, 2012 at 19:32
  • 1
    @DNA180, that probably can be achieved using regular expressions. Mar 8, 2016 at 13:24
  • 1
    It's OK I found it here here.
    – DNA180
    Mar 8, 2016 at 17:37
1

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
2
  • What does this do?
    – HackSlash
    May 23, 2018 at 15:55
  • "just cut out the crap you don't need" - You didn't explain what the code does and wouldn't it have been easier for you to crop out the code?
    – Josh Face
    Apr 1, 2020 at 14:49
0

try the following example I hope it helps

 Dim lOpenFile As Long
 Dim sFileText As String
 Dim sFileName As String

sFileName = "C:\test.txt"

 'open the file and read it into a variable
 lOpenFile = FreeFile
 Open sFileName For Input As lOpenFile
 sFileText = Input(LOF(lOpenFile), lOpenFile)
 Close lOpenFile

'change 'John Doe' to 'Mary Brown'
 sFileText = Replace(sFileText, " John Doe ", " Mary Brown ")

 'write it back to the file
 lOpenFile = FreeFile
 Open sFileName For Output As lOpenFile
 Print #lOpenFile, sFileText
 Close lOpenFile
2
  • Error 1 'Open' is not declared. File I/O functionality is available in the 'Microsoft.VisualBasic' namespace. C:\Users\EMACFEL\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb 32 9 WindowsApplication1 Apr 12, 2012 at 19:02
  • add System IO using System.IO
    – COLD TOLD
    Apr 12, 2012 at 19:20
0

I have a small program I wrote that I use for reusing code. I use it when I need to create a class, or code file, that is almost identical to an existing one. It is a basic form program with four text boxes and a button that uses the same command as the accepted answer except the strings are not hard coded.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

  Dim source As String = txtSource.Text
  Dim destination As String = txtDestination.Text
  Dim oldText As String = txtOldText.Text
  Dim newText As String = txtNewText.Text

  My.Computer.FileSystem.WriteAllText(destination, My.Computer.FileSystem.ReadAllText(source).Replace(oldText, newText), False)

End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.