VB.Net搜索文本并替换为文件内容

时间:2019-03-25 16:32:44

标签: vb.net

这是我发帖后的一个问题。 Append one file into another file

我需要在主文档中搜索实体"&CH1.sgm""&CH33.sgm", 标记它们在主文档中的位置,并将实体调用替换为在“ fnFiles”中找到的匹配文件"Chapter1.sgm"。如果有帮助,我可以将文件名和实体更改为任何内容。

我的代码复制文件的文本,并将其附加到master_document.sgm的底部。但是现在我需要它变得更聪明。在主文档中搜索实体标记,然后将该实体标记替换为与文件内容匹配的实体。文件号和实体号匹配。例如(&CH1;和Bld1_Ch1.sgm)

Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    Dim searchDir As String = txtSGMFile.Text 'Input field from form
    Dim masterFile = "Bld1_Master_Document.sgm"
    Dim existingFileMaster = Path.Combine(searchDir, masterFile)

    'Read all lines of the Master Document
    Dim strMasterDoc = File.ReadAllText(existingFileMaster) '// add each line as String Array.

    '?search strMasterDoc for entities &Ch1.sgm
    '?replace entity name "&Ch1.sgm" with content of file "Bld1_Ch1.sgm" this content if found below
    '? do I use a book mark? Replace function?


    'Get all the sgm files in the directory specified
    Dim fndFiles = Directory.GetFiles(searchDir, "*.sgm")
    'Set up the regular expression you will make as the condition for the file
    Dim rx = New Regex(".*_Ch\d\.sgm")
    Dim ch1 = New Regex(".*_Ch[1]\.sgm")
    'Use path.combine for concatenatin directory together

    'Loop through each file found by the REGEX
    For Each fileNo In fndFiles
        If rx.IsMatch(fileNo) Then
            If ch1.IsMatch(fileNo) Then
                Dim result = Path.GetFileName(fileNo)
                'Use path.combine for concatenatin directory together
                Dim fileToCopy = Path.Combine(searchDir, result)

                'This is the file we want to copy into MasterBuild but at specific location.
                'match &ch1.sgm inside strMasterDoc

                Dim fileContent = File.ReadAllText(fileToCopy)

                'Search master file for entity match then append all content of fileContent


                File.AppendAllText(existingFileMaster, fileContent)

                MessageBox.Show("File Copied")
            End If
        End If
    Next
    Close()
End Sub

1 个答案:

答案 0 :(得分:1)

如果我理解正确(如果是,则为大),您希望将主文件中缩写章节名称的文本替换为它在缩写所在处所引用的文件内容。

我制作了一个处理细节的课程。

Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    'Add a FolderBrowseDialog to your form designer
    FolderBrowserDialog1.ShowDialog()
    Dim searchDir As String = FolderBrowserDialog1.SelectedPath
    Dim existingFileMaster = Path.Combine(searchDir, "Bld1_Master_Document.sgm")
    Dim lstFileChanges = CreateList(searchDir)
    'The following method does NOT return an array of lines
    Dim strMasterDoc = File.ReadAllText(existingFileMaster)
    For Each fc In lstFileChanges
        strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
    Next
    File.WriteAllText(existingFileMaster, strMasterDoc)
End Sub

Private Function CreateList(selectedPath As String) As List(Of FileChanges)
    Dim lstFC As New List(Of FileChanges)
    For i = 1 To lstFC.Count
        Dim fc As New FileChanges
        fc.OldString = $"&CH{i}.sgm"
        fc.FileName = $"Chapter{i}.sgm"
        fc.NewString = File.ReadAllText(Path.Combine(selectedPath, fc.FileName))
        lstFC.Add(fc)
    Next
    Return lstFC
End Function

Public Class FileChanges
    Public Property OldString As String '&CH1.sgm 
    Public Property FileName As String 'Chapter1.sgm
    Public Property NewString As String 'Contents of Chapter1.sgm, the string to insert
End Class

测试。替换

Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)