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)

Why did the "Orks" never develop better firearms than Firelances and Handcannons?

Has there ever been a cold war other than between the U.S. and the U.S.S.R.?

Who pays for increased security measures on flights to the US?

How did Einstein know the speed of light was constant?

Explain how 'Sharing the burden' puzzle from Professor Layton and the Miracle Mask should be solved

Show that there are infinitely more problems than we will ever be able to compute

CPA filed late returns, stating I would get money; IRS says they were filed too late

How can power levels matter in a magic system that emphasizes control?

How can I define a very large matrix efficiently?

What can a novel do that film and TV cannot?

Did Snape really give Umbridge a fake Veritaserum potion that Harry later pretended to drink?

Two queries on triangles, the sides of which have rational lengths

Is there a typical layout to blocking installed for backing in new construction framing?

Why does the Batman "crack his knuckles" in "Batman: Arkham Origins"?

Has chattel slavery ever been used as a criminal punishment in the USA since the passage of the Thirteenth Amendment?

How do both sides know the MTU

Will electrically joined dipoles of different lengths, at right angles, behave as a multiband antenna?

What is the difference between a historical drama and a period drama?

Convenience stores in India

Why would "dead languages" be the only languages that spells could be written in?

Milky way is orbiting around?

How can solar sailed ships be protected from space debris?

Term for a character that only exists to be talked to

Park the computer



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)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-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,7117 gold badges34 silver badges54 bronze badges




8,7117 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

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript