VB.Net Search for text and replace with file contentAppend one file into another fileWhat is the difference between And and AndAlso in VB.NET?Is there a conditional ternary operator in VB.NET?Multiline strings in VB.NETClasses vs. Modules in VB.NETSearch/Replace in VB.NETReplace multiple characters in a text file using vb.netConditionally replace urls in entire HTML document (inc header)VB.NET Multiple Replace In Text Filereplace text multiple file vb.netReplace Text with Condition (VB.Net)
Is it OK to say "The situation is pregnant with a crisis"?
What is the meaning of "it" in "as luck would have it"?
What verb goes with "coup"?
What was the point of separating stdout and stderr?
Basis and cardinality
How to count the number of bytes in a file, grouping the same bytes?
Basic calculations in PGF/TikZ for loop
How does mmorpg store data?
iMac 2019: Can I mix the old modules with the new ones when upgrading RAM?
Why are symbols not written in words?
Denied entry in Croatia Border
Any Tips On Writing Extended Recollection In A Novel
Processes in a session in an interactive shell vs in a script
Does a lens with a bigger max. aperture focus faster than a lens with a smaller max. aperture?
Is it possible to alias a column based on the result of a select+where?
Could you fall off a planet if it was being accelerated by engines?
Where to connect the fuse and why?
What is my external HDD doing?
Why do movie directors use brown tint on Mexico cities?
Do electrons really perform instantaneous quantum leaps?
My mom helped me cosign a car and now she wants to take it
Why will we fail creating a self sustaining off world colony?
Reaction mechanism of rearrangement
Is there a word for the act of simultaneously pulling and twisting an object?
VB.Net Search for text and replace with file content
Append one file into another fileWhat is the difference between And and AndAlso in VB.NET?Is there a conditional ternary operator in VB.NET?Multiline strings in VB.NETClasses vs. Modules in VB.NETSearch/Replace in VB.NETReplace multiple characters in a text file using vb.netConditionally replace urls in entire HTML document (inc header)VB.NET Multiple Replace In Text Filereplace text multiple file vb.netReplace Text with Condition (VB.Net)
This is a follow on question to a post I made. Append one file into another file
I need to search the master document for entities "&CH1.sgm"
to "&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)
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(".*_Chd.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
vb.net
add a comment |
This is a follow on question to a post I made. Append one file into another file
I need to search the master document for entities "&CH1.sgm"
to "&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)
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(".*_Chd.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
vb.net
IMO, you should use a Regex to find matching patterns inside your Master Document (&CH1.sgm
,&CH2.sgm
etc.).Regex.Matches
returns a collection of matches which reports the index position inside the string where the match was found and also (among other things) the length of the matched pattern. You can then use these informations toInsert
a string (corresponding to an external file content) in the referenced positions. Use the pattern found to load the related Text file. Use aStringBuilder
instead of a simplestring
to hold the files content.
– Jimi
Mar 25 at 18:15
It looks like you have just pasted the fixed code from the other question, but I don't see a trace of an attempt to achieve your new requirement. Try to solve it yourself and come back with any specific issues you encounter.
– djv
Mar 25 at 18:42
I have been working on it and searching the net for an answer. What I posted was the cleanest version of my file so you wouldn't see all my previous attempts and mistakes. I wouldn't just paste code an expect everyone to write it for me. But I appreciate your wanting to keep me honest. Max
– Maxine Hammett
Mar 25 at 19:58
add a comment |
This is a follow on question to a post I made. Append one file into another file
I need to search the master document for entities "&CH1.sgm"
to "&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)
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(".*_Chd.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
vb.net
This is a follow on question to a post I made. Append one file into another file
I need to search the master document for entities "&CH1.sgm"
to "&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)
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(".*_Chd.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
vb.net
vb.net
edited Mar 25 at 18:38
djv
8,6767 gold badges34 silver badges54 bronze badges
8,6767 gold badges34 silver badges54 bronze badges
asked Mar 25 at 16:32
Maxine HammettMaxine Hammett
10910 bronze badges
10910 bronze badges
IMO, you should use a Regex to find matching patterns inside your Master Document (&CH1.sgm
,&CH2.sgm
etc.).Regex.Matches
returns a collection of matches which reports the index position inside the string where the match was found and also (among other things) the length of the matched pattern. You can then use these informations toInsert
a string (corresponding to an external file content) in the referenced positions. Use the pattern found to load the related Text file. Use aStringBuilder
instead of a simplestring
to hold the files content.
– Jimi
Mar 25 at 18:15
It looks like you have just pasted the fixed code from the other question, but I don't see a trace of an attempt to achieve your new requirement. Try to solve it yourself and come back with any specific issues you encounter.
– djv
Mar 25 at 18:42
I have been working on it and searching the net for an answer. What I posted was the cleanest version of my file so you wouldn't see all my previous attempts and mistakes. I wouldn't just paste code an expect everyone to write it for me. But I appreciate your wanting to keep me honest. Max
– Maxine Hammett
Mar 25 at 19:58
add a comment |
IMO, you should use a Regex to find matching patterns inside your Master Document (&CH1.sgm
,&CH2.sgm
etc.).Regex.Matches
returns a collection of matches which reports the index position inside the string where the match was found and also (among other things) the length of the matched pattern. You can then use these informations toInsert
a string (corresponding to an external file content) in the referenced positions. Use the pattern found to load the related Text file. Use aStringBuilder
instead of a simplestring
to hold the files content.
– Jimi
Mar 25 at 18:15
It looks like you have just pasted the fixed code from the other question, but I don't see a trace of an attempt to achieve your new requirement. Try to solve it yourself and come back with any specific issues you encounter.
– djv
Mar 25 at 18:42
I have been working on it and searching the net for an answer. What I posted was the cleanest version of my file so you wouldn't see all my previous attempts and mistakes. I wouldn't just paste code an expect everyone to write it for me. But I appreciate your wanting to keep me honest. Max
– Maxine Hammett
Mar 25 at 19:58
IMO, you should use a Regex to find matching patterns inside your Master Document (
&CH1.sgm
, &CH2.sgm
etc.). Regex.Matches
returns a collection of matches which reports the index position inside the string where the match was found and also (among other things) the length of the matched pattern. You can then use these informations to Insert
a string (corresponding to an external file content) in the referenced positions. Use the pattern found to load the related Text file. Use a StringBuilder
instead of a simple string
to hold the files content.– Jimi
Mar 25 at 18:15
IMO, you should use a Regex to find matching patterns inside your Master Document (
&CH1.sgm
, &CH2.sgm
etc.). Regex.Matches
returns a collection of matches which reports the index position inside the string where the match was found and also (among other things) the length of the matched pattern. You can then use these informations to Insert
a string (corresponding to an external file content) in the referenced positions. Use the pattern found to load the related Text file. Use a StringBuilder
instead of a simple string
to hold the files content.– Jimi
Mar 25 at 18:15
It looks like you have just pasted the fixed code from the other question, but I don't see a trace of an attempt to achieve your new requirement. Try to solve it yourself and come back with any specific issues you encounter.
– djv
Mar 25 at 18:42
It looks like you have just pasted the fixed code from the other question, but I don't see a trace of an attempt to achieve your new requirement. Try to solve it yourself and come back with any specific issues you encounter.
– djv
Mar 25 at 18:42
I have been working on it and searching the net for an answer. What I posted was the cleanest version of my file so you wouldn't see all my previous attempts and mistakes. I wouldn't just paste code an expect everyone to write it for me. But I appreciate your wanting to keep me honest. Max
– Maxine Hammett
Mar 25 at 19:58
I have been working on it and searching the net for an answer. What I posted was the cleanest version of my file so you wouldn't see all my previous attempts and mistakes. I wouldn't just paste code an expect everyone to write it for me. But I appreciate your wanting to keep me honest. Max
– Maxine Hammett
Mar 25 at 19:58
add a comment |
1 Answer
1
active
oldest
votes
If I understand correctly (big if), you want to replace the the text of the abbreviated chapter name in the master file with the contents of the file it refers to at the spot where the abbreviation is found.
I made a class to handle the details.
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 = $"&CHi.sgm"
fc.FileName = $"Chapteri.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
Testing .Replace
Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
.For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)
– Jimi
Mar 25 at 20:10
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
|
show 19 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342437%2fvb-net-search-for-text-and-replace-with-file-content%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If I understand correctly (big if), you want to replace the the text of the abbreviated chapter name in the master file with the contents of the file it refers to at the spot where the abbreviation is found.
I made a class to handle the details.
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 = $"&CHi.sgm"
fc.FileName = $"Chapteri.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
Testing .Replace
Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
.For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)
– Jimi
Mar 25 at 20:10
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
|
show 19 more comments
If I understand correctly (big if), you want to replace the the text of the abbreviated chapter name in the master file with the contents of the file it refers to at the spot where the abbreviation is found.
I made a class to handle the details.
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 = $"&CHi.sgm"
fc.FileName = $"Chapteri.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
Testing .Replace
Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
.For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)
– Jimi
Mar 25 at 20:10
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
|
show 19 more comments
If I understand correctly (big if), you want to replace the the text of the abbreviated chapter name in the master file with the contents of the file it refers to at the spot where the abbreviation is found.
I made a class to handle the details.
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 = $"&CHi.sgm"
fc.FileName = $"Chapteri.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
Testing .Replace
Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)
If I understand correctly (big if), you want to replace the the text of the abbreviated chapter name in the master file with the contents of the file it refers to at the spot where the abbreviation is found.
I made a class to handle the details.
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 = $"&CHi.sgm"
fc.FileName = $"Chapteri.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
Testing .Replace
Dim s As String = "The quick brown fox jumped over the lazy dogs."
s = s.Replace("fox", "foxes")
MessageBox.Show(s)
edited Mar 25 at 23:08
answered Mar 25 at 19:21
MaryMary
4,8492 gold badges10 silver badges22 bronze badges
4,8492 gold badges10 silver badges22 bronze badges
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
.For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)
– Jimi
Mar 25 at 20:10
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
|
show 19 more comments
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
.For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)
– Jimi
Mar 25 at 20:10
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
. For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)– Jimi
Mar 25 at 20:10
strMasterDoc = strMasterDoc.Replace(fc.OldString, fc.NewString)
. For i = 1 To 33
? Uhmm. So, you counted them beforehand? (I know, the question says that..., but...)– Jimi
Mar 25 at 20:10
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
@Mary thank you this was great, but it's not replacing the text in in strMasterDoc.Replace(fc.OldString, fc.NewString). If I do a dialogue box check to see what's in each variable. It's right it's grabbing the right text.
– Maxine Hammett
Mar 25 at 20:27
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
strMasterDoc count is infinite. I just put 33 as a jumping off point
– Maxine Hammett
Mar 25 at 20:29
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@Jimi changed to lstFC.Count. Thanks
– Mary
Mar 25 at 20:41
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
@MaxineHammett changed 33 to lstFC.Count
– Mary
Mar 25 at 20:43
|
show 19 more comments
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55342437%2fvb-net-search-for-text-and-replace-with-file-content%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
IMO, you should use a Regex to find matching patterns inside your Master Document (
&CH1.sgm
,&CH2.sgm
etc.).Regex.Matches
returns a collection of matches which reports the index position inside the string where the match was found and also (among other things) the length of the matched pattern. You can then use these informations toInsert
a string (corresponding to an external file content) in the referenced positions. Use the pattern found to load the related Text file. Use aStringBuilder
instead of a simplestring
to hold the files content.– Jimi
Mar 25 at 18:15
It looks like you have just pasted the fixed code from the other question, but I don't see a trace of an attempt to achieve your new requirement. Try to solve it yourself and come back with any specific issues you encounter.
– djv
Mar 25 at 18:42
I have been working on it and searching the net for an answer. What I posted was the cleanest version of my file so you wouldn't see all my previous attempts and mistakes. I wouldn't just paste code an expect everyone to write it for me. But I appreciate your wanting to keep me honest. Max
– Maxine Hammett
Mar 25 at 19:58