我正在寻找使用visual studio 2015社区工具构建一个简单的文本字符串替换工具,该工具将对所有* .txt文件进行以下替换,其路径在文本框中给出:
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>
我编写了一小部分程序,但很难完成它。我在编程和视觉基础方面都是全新的。任何人都可以帮助完成这个程序
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
我得到的错误在下面给出
Error BC30456 'ReadAllLines' is not a member of 'String'
答案 0 :(得分:2)
对于替换按钮,一旦有了指向文件夹目录的链接,就需要读入所有说&#34; * .txt&#34;的文件。以下行执行此操作
targetDirectory = TextBox1.text
Dim txtFilesArray As String() = Directory.GetFiles(targetDirectory,"*.txt")
然后,您可以遍历此数组并执行替换逻辑。
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")
答案 1 :(得分:-1)
@Tamal Banerjee,尝试使用进度条,将以下代码放在编码中的最后一个Next
Next
ProgressBar1.PerformStep()
ProgressBar1.Value = 100
MessageBox.Show("Process complete")
End Sub
我不确定这是否是这样做的写入方式,但是当我在我的计算机上使用您的编码尝试这种方法时它起作用了:)