Tamal Banerjee Tamal Banerjee - 3 months ago 18
Vb.net Question

Replacing strings in multiple text files using regular expressions on vb.net

I'm looking to build a simple text string replacement tool using visual studio 2015 community tool, which will do the below replacements on all *.txt files whose path is given in a textbox:

Find: \<figure (\d+)\>
Replace: <a href id="fig\1">figure \1</a>

Find: \<table (\d+)\>
Replace: <a href id="tab\1">table \1</a>

Find: \<section (\d+)\>
Replace: <a href id="sec\1">section \1</a>


I have coded a little portion of the programme but struggling to complete it. I'm completely new in programming and in visual basic is well. Can anyone help complete this programme

Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FBD.ShowDialog = DialogResult.OK Then
TextBox1.Text = FBD.SelectedPath
End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim targetDirectory As String
targetDirectory = TextBox1.Text
Dim Files As String() = Directory.GetFiles(targetDirectory, "*.txt")
For Each file In Files
Dim FileInfo As New FileInfo(file)
Dim FileLocation As String = FileInfo.FullName
Dim input As String = file.ReadAllLines(FileLocation)
Dim pattern1 As String = "\<figure (\d+)\>"
Dim pattern2 As String = "\<table (\d+)\>"
Dim pattern3 As String = "\<section (\d+)\>"
Dim rep1 As String = "<a href id= \""fig\1\"" > figure \1</a>"
Dim rep2 As String = "<a href id= \""tab\1\"" > table \1</a>"
Dim rep3 As String = "<a href id= \""sec\1\"" > section \1</a>"
Dim rgx1 As New Regex(pattern1)
Dim rgx2 As New Regex(pattern2)
Dim rgx3 As New Regex(pattern3)
Dim result1 As String = rgx1.Replace(input, rep1)
Dim result2 As String = rgx2.Replace(result1, rep2)
Dim result3 As String = rgx3.Replace(result2, rep3)
Next
End Sub
End Class


The errors I'm getting are given below

Error BC30456 'ReadAllLines' is not a member of 'String'

Answer

For the replace button, once you have the link to the folder directory, you need to read in all files that say "*.txt". The below line does this

targetDirectory = TextBox1.text
Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory,"*.txt")

You can then loop through this array and do your replace logic.

    For each txtFile in txtFilesArray
        'here we grab the files information as we need the files directory
        Dim FileInfo As New FileInfo(txtFile)
        Dim FileLocation As String = FileInfo.FullName
        Dim input() as string = File.ReadAllLines(FileLocation)
        'now you have read in your text file you can edit each file as it goes through the loop
        'you can now use your regex here to edit each file, 
        'then once done editing the file, dont forget to write back to your file or it wont save
        'you will need to loop through the input array now to change the line
       For x as integer = 0 to (input.length - 1)
         Dim pattern1 As String = "\<figure (\d+)\>"
         Dim pattern2 As String = "\<table (\d+)\>"
         Dim pattern3 As String = "\<section (\d+)\>"
         Dim rep1 As String = "<a href id= \""fig\1\"" > figure \1</a>"
         Dim rep2 As String = "<a href id= \""tab\1\"" > table \1</a>"
         Dim rep3 As String = "<a href id= \""sec\1\"" > section \1</a>"
         Dim rgx1 As New Regex(pattern1)
         Dim rgx2 As New Regex(pattern2)
         Dim rgx3 As New Regex(pattern3)
         Dim result1 As String = rgx1.Replace(input(x), rep1)
         Dim result2 As String = rgx2.Replace(result1, rep2)
         Dim result3 As String = rgx3.Replace(result2, rep3)
         input(x) = result3
       Next
      'now you can write your corrected file back to the file
      File.WriteAllLines(FileLocation, input)
    Next

  MsgBox("process complete")
Comments