Results 1 to 17 of 17

Thread: Search file for string and append file to it

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Red face Search file for string and append file to it

    This is a follow on question to a post I made. https://stackoverflow.com/questions/...40628#55340628

    I need to search the master document for entities...&CH1.sgm - &CH33.sgm
    Mark where they are in the master document and replace the entity call with the matching file "Chapter1.sgm" found in "fnFiles". I can change the file names and entities to anything if that will help.

    My code copies the text of a file and appends it to the bottom of the master_document.sgm. But now I need it to be more intelligent. Search the Master document for entity markers, then replace that entity marker with that file contents match. The file number and entity number match up. e.g.(&CH1; and Bld1_Ch1.sgm)

    Thank you for all your help. You guys are great


    Code:
        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

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Search file for string and append file to it

    Could easily do this with a regex ... but don't know what you mean by this:
    Quote Originally Posted by Mightymax View Post
    The file number and entity number match up. e.g.(&CH1; and Bld1_Ch1.sgm
    .. What is the entity number? ... also what is &CH1; vs Bld1_Ch1.sgm ... and should Bld1_Ch1 have an & before it?

    Kris

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    &CH1; is the entity placeholder and Bld1_Ch1.sgm is the file with the contents I wan to copy in the master.

    Here's the latest in my code. I'm having trouble getting the replace function to work on strMasterDoc.

    Code:
    Imports System
    Imports System.IO
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Text.RegularExpressions
    
    
    Public Class Form1
        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
            MsgBox("Search Dir " & searchDir)
            Dim existingFileMaster = Path.Combine(searchDir, "Bld1_Master_Document.sgm")
            MsgBox("fOUND MasterBuild file " & existingFileMaster)
            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)
            Dim count As Integer = Directory.GetFiles(selectedPath, "*.sgm").Count-1
            'MsgBox("COUNT  " & count)
            For i = 1 To count
                Dim fc As New FileChanges
                fc.OldString = $"&CH{i};"
                fc.FileName = $"Chapter{i}.sgm"
                fc.NewString = File.ReadAllText(Path.Combine(selectedPath, fc.FileName))
                lstFC.Add(fc)
            Next
            Return lstFC
        End Function
    End Class
    
    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

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Red face Re: Search file for string and append file to it

    &CH1; is the entity placeholder and Bld1_Ch1.sgm is the file with the contents I want to copy in the master.

    Here's the latest in my code. I'm having trouble getting the replace function to work on strMasterDoc.

    Code:
    Imports System
    Imports System.IO
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Text.RegularExpressions
    
    
    Public Class Form1
        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
            MsgBox("Search Dir " & searchDir)
            Dim existingFileMaster = Path.Combine(searchDir, "Bld1_Master_Document.sgm")
            MsgBox("fOUND MasterBuild file " & existingFileMaster)
            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)
            Dim count As Integer = Directory.GetFiles(selectedPath, "*.sgm").Count-1
            'MsgBox("COUNT  " & count)
            For i = 1 To count
                Dim fc As New FileChanges
                fc.OldString = $"&CH{i};"
                fc.FileName = $"Chapter{i}.sgm"
                fc.NewString = File.ReadAllText(Path.Combine(selectedPath, fc.FileName))
                lstFC.Add(fc)
            Next
            Return lstFC
        End Function
    End Class
    
    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

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Search file for string and append file to it

    Quote Originally Posted by Mightymax View Post
    &CH1; is the entity placeholder and Bld1_Ch1.sgm is the file with the contents I want to copy in the master.

    You could use a regex search to find all the matches for the entity placeholders in the master file.

    You can use the same regex search to capture the entity digits, and use those digits to build the name of the file that corresponds to the entity place holder (i.e. &CH1; uses file Bld1_Ch1.sgm, and &CH63; uses file Bld1_Ch63.sgm, and so on).

    Then create a StringBuilder from the contents of the master file.Loop through the regex matches that were found and use the StringBuilder's Replace method to replace each of the regex matches (i.e. the &CH###; entities) with the contents of the corresponding Bld1_Ch###.sgm file.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    I'm afraid I wouldn't know how to do that. I'm comfortable with regular expressions but very new to VB.NET. Is this something you'd be willing to work with me on?

  7. #7
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Search file for string and append file to it

    Quote Originally Posted by Mightymax View Post
    I'm afraid I wouldn't know how to do that. I'm comfortable with regular expressions but very new to VB.NET. Is this something you'd be willing to work with me on?
    Happy to try.

    Based on my understanding (which could be wrong) of what you have described so far and your original code, this is what I had in mind:
    VB.Net Code:
    1. Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    2.     'Add a FolderBrowseDialog to your form designer
    3.     FolderBrowserDialog1.ShowDialog()
    4.     Dim searchDir As String = FolderBrowserDialog1.SelectedPath
    5.  
    6.     Dim masterFileName = "Bld1_Master_Document.sgm"
    7.     Dim newMasterFileName = "Bld1_New_Master_Document.sgm"   ' file name of output file
    8.  
    9.     Dim existingMasterFilePath = Path.Combine(searchDir, masterFileName)
    10.     Dim newMasterFilePath = Path.Combine(searchDir, newMasterFileName)
    11.  
    12.  
    13.     'Read all text of the Master Document
    14.     'and create a StringBuilder from it.
    15.     'All replacements will be done on the
    16.     'StringBuilder as it is more efficient
    17.     'than using Strings directly
    18.     Dim strMasterDoc = File.ReadAllText(existingMasterFilePath)
    19.     Dim newMasterFileBuilder As New StringBuilder(strMasterDoc)
    20.  
    21.     'Create a regex with a named capture group.
    22.     'The name is 'EntityNumber' and captures just the
    23.     'entity digits for use in building the file name
    24.     Dim rx = New Regex("&CH(?<EntityNumber>\d+?);")
    25.     Dim rxMatches = rx.Matches(strMasterDoc)
    26.  
    27.     For Each match As Match In rxMatches
    28.         Dim entity = match.ToString
    29.         'Build the file name using the captured digits from the entity in the master file
    30.         Dim entityFileName = $"Bld1_Ch{match.Groups("EntityNumber")}.sgm"
    31.         Dim entityFilePath = Path.Combine(searchDir, entityFileName)
    32.         'Check if the entity file exists and use its contents
    33.         'to replace the entity in the copy of the master file
    34.         'contained in the StringBuilder
    35.         If File.Exists(entityFilePath) Then
    36.             Dim entityFileContents As String = File.ReadAllText(entityFilePath)
    37.             newMasterFileBuilder.Replace(entity, entityFileContents)
    38.         End If
    39.  
    40.     Next
    41.  
    42.     'write the processed contents of the master file to a different file
    43.     File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString)
    44.  
    45. End Sub
    Note that I'm writing the contents of the updated master file to a different file. If you wish, you can overwrite the original file just as you were doing in your original code.

    If there's anything you don't understand, please ask.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    Quote Originally Posted by Inferrd View Post
    Happy to try.

    Based on my understanding (which could be wrong) of what you have described so far and your original code, this is what I had in mind:
    VB.Net Code:
    1. Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    2.     'Add a FolderBrowseDialog to your form designer
    3.     FolderBrowserDialog1.ShowDialog()
    4.     Dim searchDir As String = FolderBrowserDialog1.SelectedPath
    5.  
    6.     Dim masterFileName = "Bld1_Master_Document.sgm"
    7.     Dim newMasterFileName = "Bld1_New_Master_Document.sgm"   ' file name of output file
    8.  
    9.     Dim existingMasterFilePath = Path.Combine(searchDir, masterFileName)
    10.     Dim newMasterFilePath = Path.Combine(searchDir, newMasterFileName)
    11.  
    12.  
    13.     'Read all text of the Master Document
    14.     'and create a StringBuilder from it.
    15.     'All replacements will be done on the
    16.     'StringBuilder as it is more efficient
    17.     'than using Strings directly
    18.     Dim strMasterDoc = File.ReadAllText(existingMasterFilePath)
    19.     Dim newMasterFileBuilder As New StringBuilder(strMasterDoc)
    20.  
    21.     'Create a regex with a named capture group.
    22.     'The name is 'EntityNumber' and captures just the
    23.     'entity digits for use in building the file name
    24.     Dim rx = New Regex("&CH(?<EntityNumber>\d+?);")
    25.     Dim rxMatches = rx.Matches(strMasterDoc)
    26.  
    27.     For Each match As Match In rxMatches
    28.         Dim entity = match.ToString
    29.         'Build the file name using the captured digits from the entity in the master file
    30.         Dim entityFileName = $"Bld1_Ch{match.Groups("EntityNumber")}.sgm"
    31.         Dim entityFilePath = Path.Combine(searchDir, entityFileName)
    32.         'Check if the entity file exists and use its contents
    33.         'to replace the entity in the copy of the master file
    34.         'contained in the StringBuilder
    35.         If File.Exists(entityFilePath) Then
    36.             Dim entityFileContents As String = File.ReadAllText(entityFilePath)
    37.             newMasterFileBuilder.Replace(entity, entityFileContents)
    38.         End If
    39.  
    40.     Next
    41.  
    42.     'write the processed contents of the master file to a different file
    43.     File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString)
    44.  
    45. End Sub
    Note that I'm writing the contents of the updated master file to a different file. If you wish, you can overwrite the original file just as you were doing in your original code.

    If there's anything you don't understand, please ask.
    Thank you I'll look through this right now! I really appreciate this.

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Red face Re: Search file for string and append file to it

    Quote Originally Posted by Inferrd View Post
    Happy to try.

    Based on my understanding (which could be wrong) of what you have described so far and your original code, this is what I had in mind:
    VB.Net Code:
    1. Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
    2.     'Add a FolderBrowseDialog to your form designer
    3.     FolderBrowserDialog1.ShowDialog()
    4.     Dim searchDir As String = FolderBrowserDialog1.SelectedPath
    5.  
    6.     Dim masterFileName = "Bld1_Master_Document.sgm"
    7.     Dim newMasterFileName = "Bld1_New_Master_Document.sgm"   ' file name of output file
    8.  
    9.     Dim existingMasterFilePath = Path.Combine(searchDir, masterFileName)
    10.     Dim newMasterFilePath = Path.Combine(searchDir, newMasterFileName)
    11.  
    12.  
    13.     'Read all text of the Master Document
    14.     'and create a StringBuilder from it.
    15.     'All replacements will be done on the
    16.     'StringBuilder as it is more efficient
    17.     'than using Strings directly
    18.     Dim strMasterDoc = File.ReadAllText(existingMasterFilePath)
    19.     Dim newMasterFileBuilder As New StringBuilder(strMasterDoc)
    20.  
    21.     'Create a regex with a named capture group.
    22.     'The name is 'EntityNumber' and captures just the
    23.     'entity digits for use in building the file name
    24.     Dim rx = New Regex("&CH(?<EntityNumber>\d+?);")
    25.     Dim rxMatches = rx.Matches(strMasterDoc)
    26.  
    27.     For Each match As Match In rxMatches
    28.         Dim entity = match.ToString
    29.         'Build the file name using the captured digits from the entity in the master file
    30.         Dim entityFileName = $"Bld1_Ch{match.Groups("EntityNumber")}.sgm"
    31.         Dim entityFilePath = Path.Combine(searchDir, entityFileName)
    32.         'Check if the entity file exists and use its contents
    33.         'to replace the entity in the copy of the master file
    34.         'contained in the StringBuilder
    35.         If File.Exists(entityFilePath) Then
    36.             Dim entityFileContents As String = File.ReadAllText(entityFilePath)
    37.             newMasterFileBuilder.Replace(entity, entityFileContents)
    38.         End If
    39.  
    40.     Next
    41.  
    42.     'write the processed contents of the master file to a different file
    43.     File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString)
    44.  
    45. End Sub
    Note that I'm writing the contents of the updated master file to a different file. If you wish, you can overwrite the original file just as you were doing in your original code.

    If there's anything you don't understand, please ask.
    Thank you I'll look through this right now! I really appreciate this.

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    I loaded the code and wasn't able to get it to work. The newMasterFileBuilder copies the existing master file. The rxMatches finds "&Ch1; &Ch2; &Ch3; &Ch4; &Ch5; &Ch6; &Ch7; &Ch8;" But then nothing happens. It doesn't go into the For Loop.

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    entity number is a place holder for a file. It's called out as &Name;. It's not the same as Ch1.sgm, that's the file the entity is referencing

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Search file for string and append file to it

    Quote Originally Posted by Mightymax View Post
    I loaded the code and wasn't able to get it to work. The newMasterFileBuilder copies the existing master file. The rxMatches finds "&Ch1; &Ch2; &Ch3; &Ch4; &Ch5; &Ch6; &Ch7; &Ch8;" But then nothing happens. It doesn't go into the For Loop.
    If rxMatches does find &Ch1; through &Ch8;, then rxMatches.Count should = 8 and the For loop must be entered. Place a breakpoint on the line For Each match As Match In rxMatches then step through the code and see what happens.


    Quote Originally Posted by Mightymax View Post
    entity number is a place holder for a file. It's called out as &Name;. It's not the same as Ch1.sgm, that's the file the entity is referencing
    So the corresponding files are named 'Ch1.sgm' and not 'Bld1_Ch1.sgm' ?

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    No corresponding file names are "Bld1_Ch1.sgm". But the "Bld1" is a unique identifier and will change with each group of file processed.

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    I put the break point at the for each loop it never makes it past the for line. The rxMatches variable contains the whole strMasterDoc file not just the &Ch1; entities.

    I am unfamiliar with how your checking your regex I've never seen the use of <name> in a regex statement before. Can you please explain?

  15. #15

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    OH IT WORKED! THANK YOU SO MUCH!!!


    QUESTION:
    I am unfamiliar with how your checking your regex I've never seen the use of <name> in a regex statement before. Can you please explain?
    Last edited by Mightymax; Mar 27th, 2019 at 08:29 AM.

  16. #16

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    Quote Originally Posted by Inferrd View Post
    So the corresponding files are named 'Ch1.sgm' and not 'Bld1_Ch1.sgm' ?
    The file name will always have a unique prefix to it. I thought I could put a wild card before Ch but this didn't work.

    Code:
    Dim entityFileName = $".*?_Ch{match.Groups("EntityNumber")}.sgm"
    But this didn't work. How would I get the code to recognize the prefix?

    Thank you for all your help and patience
    Maxine

  17. #17

    Thread Starter
    Member
    Join Date
    Mar 2019
    Posts
    32

    Re: Search file for string and append file to it

    Quote Originally Posted by Inferrd View Post
    So the corresponding files are named 'Ch1.sgm' and not 'Bld1_Ch1.sgm' ?
    The file name will always have a unique prefix to it. I thought I could put a wild card before Ch but this didn't work.

    Code:
    Dim entityFileName = $".*?_Ch{match.Groups("EntityNumber")}.sgm"
    But this didn't work. How would I get the code to recognize the prefix?

    Thank you for all your help and patience
    Maxine

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width