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)













-3















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









share|improve this question
























  • 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











  • 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















-3















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









share|improve this question
























  • 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











  • 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













-3












-3








-3








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









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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











  • 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












  • 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










1 Answer
1






active

oldest

votes


















1














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)





share|improve this answer

























  • 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










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
);



);













draft saved

draft discarded


















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









1














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)





share|improve this answer

























  • 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















1














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)





share|improve this answer

























  • 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













1












1








1







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)





share|improve this answer















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)






share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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






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.



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현