-
Mar 25th, 2019, 12:02 PM
#1
Thread Starter
Member
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
-
Mar 26th, 2019, 01:25 AM
#2
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:
Originally Posted by Mightymax
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
-
Mar 26th, 2019, 09:02 AM
#3
Thread Starter
Member
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
-
Mar 26th, 2019, 09:04 AM
#4
Thread Starter
Member
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
-
Mar 26th, 2019, 01:03 PM
#5
Re: Search file for string and append file to it
Originally Posted by Mightymax
&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.
-
Mar 26th, 2019, 01:10 PM
#6
Thread Starter
Member
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?
-
Mar 26th, 2019, 01:23 PM
#7
Re: Search file for string and append file to it
Originally Posted by Mightymax
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:
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 masterFileName = "Bld1_Master_Document.sgm" Dim newMasterFileName = "Bld1_New_Master_Document.sgm" ' file name of output file Dim existingMasterFilePath = Path.Combine(searchDir, masterFileName) Dim newMasterFilePath = Path.Combine(searchDir, newMasterFileName) 'Read all text of the Master Document 'and create a StringBuilder from it. 'All replacements will be done on the 'StringBuilder as it is more efficient 'than using Strings directly Dim strMasterDoc = File.ReadAllText(existingMasterFilePath) Dim newMasterFileBuilder As New StringBuilder(strMasterDoc) 'Create a regex with a named capture group. 'The name is 'EntityNumber' and captures just the 'entity digits for use in building the file name Dim rx = New Regex("&CH(?<EntityNumber>\d+?);") Dim rxMatches = rx.Matches(strMasterDoc) For Each match As Match In rxMatches Dim entity = match.ToString 'Build the file name using the captured digits from the entity in the master file Dim entityFileName = $"Bld1_Ch{match.Groups("EntityNumber")}.sgm" Dim entityFilePath = Path.Combine(searchDir, entityFileName) 'Check if the entity file exists and use its contents 'to replace the entity in the copy of the master file 'contained in the StringBuilder If File.Exists(entityFilePath) Then Dim entityFileContents As String = File.ReadAllText(entityFilePath) newMasterFileBuilder.Replace(entity, entityFileContents) End If Next 'write the processed contents of the master file to a different file File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString) 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.
-
Mar 26th, 2019, 03:29 PM
#8
Thread Starter
Member
Re: Search file for string and append file to it
Originally Posted by Inferrd
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:
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 masterFileName = "Bld1_Master_Document.sgm" Dim newMasterFileName = "Bld1_New_Master_Document.sgm" ' file name of output file Dim existingMasterFilePath = Path.Combine(searchDir, masterFileName) Dim newMasterFilePath = Path.Combine(searchDir, newMasterFileName) 'Read all text of the Master Document 'and create a StringBuilder from it. 'All replacements will be done on the 'StringBuilder as it is more efficient 'than using Strings directly Dim strMasterDoc = File.ReadAllText(existingMasterFilePath) Dim newMasterFileBuilder As New StringBuilder(strMasterDoc) 'Create a regex with a named capture group. 'The name is 'EntityNumber' and captures just the 'entity digits for use in building the file name Dim rx = New Regex("&CH(?<EntityNumber>\d+?);") Dim rxMatches = rx.Matches(strMasterDoc) For Each match As Match In rxMatches Dim entity = match.ToString 'Build the file name using the captured digits from the entity in the master file Dim entityFileName = $"Bld1_Ch{match.Groups("EntityNumber")}.sgm" Dim entityFilePath = Path.Combine(searchDir, entityFileName) 'Check if the entity file exists and use its contents 'to replace the entity in the copy of the master file 'contained in the StringBuilder If File.Exists(entityFilePath) Then Dim entityFileContents As String = File.ReadAllText(entityFilePath) newMasterFileBuilder.Replace(entity, entityFileContents) End If Next 'write the processed contents of the master file to a different file File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString) 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.
-
Mar 26th, 2019, 03:31 PM
#9
Thread Starter
Member
Re: Search file for string and append file to it
Originally Posted by Inferrd
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:
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 masterFileName = "Bld1_Master_Document.sgm"
Dim newMasterFileName = "Bld1_New_Master_Document.sgm" ' file name of output file
Dim existingMasterFilePath = Path.Combine(searchDir, masterFileName)
Dim newMasterFilePath = Path.Combine(searchDir, newMasterFileName)
'Read all text of the Master Document
'and create a StringBuilder from it.
'All replacements will be done on the
'StringBuilder as it is more efficient
'than using Strings directly
Dim strMasterDoc = File.ReadAllText(existingMasterFilePath)
Dim newMasterFileBuilder As New StringBuilder(strMasterDoc)
'Create a regex with a named capture group.
'The name is 'EntityNumber' and captures just the
'entity digits for use in building the file name
Dim rx = New Regex("&CH(?<EntityNumber>\d+?);")
Dim rxMatches = rx.Matches(strMasterDoc)
For Each match As Match In rxMatches
Dim entity = match.ToString
'Build the file name using the captured digits from the entity in the master file
Dim entityFileName = $"Bld1_Ch{match.Groups("EntityNumber")}.sgm"
Dim entityFilePath = Path.Combine(searchDir, entityFileName)
'Check if the entity file exists and use its contents
'to replace the entity in the copy of the master file
'contained in the StringBuilder
If File.Exists(entityFilePath) Then
Dim entityFileContents As String = File.ReadAllText(entityFilePath)
newMasterFileBuilder.Replace(entity, entityFileContents)
End If
Next
'write the processed contents of the master file to a different file
File.WriteAllText(newMasterFilePath, newMasterFileBuilder.ToString)
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.
-
Mar 26th, 2019, 04:30 PM
#10
Thread Starter
Member
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.
-
Mar 26th, 2019, 04:31 PM
#11
Thread Starter
Member
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
-
Mar 26th, 2019, 05:15 PM
#12
Re: Search file for string and append file to it
Originally Posted by Mightymax
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.
Originally Posted by Mightymax
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' ?
-
Mar 27th, 2019, 07:52 AM
#13
Thread Starter
Member
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.
-
Mar 27th, 2019, 08:19 AM
#14
Thread Starter
Member
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?
-
Mar 27th, 2019, 08:24 AM
#15
Thread Starter
Member
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.
-
Mar 27th, 2019, 08:43 AM
#16
Thread Starter
Member
Re: Search file for string and append file to it
Originally Posted by Inferrd
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
-
Mar 27th, 2019, 08:46 AM
#17
Thread Starter
Member
Re: Search file for string and append file to it
Originally Posted by Inferrd
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|