How do I replace text from one file with text from another file using vbscript?
如何使用vbscript用另一个文件中的文本替换一个文件中的文本?
The text being replaced is somewhere in the middle of the file.
被替换的文本位于文件中间的某个位置。
2 个解决方案
#1
4
filea.txt: hello cruel world
filea.txt:你好残酷的世界
fileb.txt: cruel
filec.txt: happy
will make sResult = "hello happy world" after the following has executed.
在执行以下操作后,将使sResult =“hello happy world”。
Dim oFSO
Dim sFileAContents
Dim sFileBContents
Dim sFileCContents
Dim sResult
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFileAContents = oFSO.OpenTextFile("c:\filea.txt").ReadAll()
sFileBContents = oFSO.OpenTextFile("c:\fileb.txt").ReadAll()
sFileCContents = oFSO.OpenTextFile("c:\filec.txt").ReadAll()
sResult = Replace(sFileAContents, sFileBContents, "")
#2
0
FileToSearch is the file with the text you want to search for replacement
FileReplaceText is the file containing the replacement text
FileToSearch是包含要搜索替换文本的文件FileReplaceText是包含替换文本的文件
Edit the value of the variable strTextToFind to contain the text you are searching for and replacing
编辑变量strTextToFind的值以包含要搜索和替换的文本
Dim objFSO
Dim strFileToSearch
Dim strFileReplaceText
Dim strTextToFind
Dim strTextToSearch
Dim strTextReplaceText
Dim strFinalText
strFileToSearch = "C:\FileToSearch.txt"
strFileReplaceText = "C:\FileReplaceText.txt"
strTextToFind = "text to search for here"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextToSearch = objFSO.OpenTextFile(strFileToSearch).ReadAll()
strFileReplaceText = objFSO.OpenTextFile(strFileReplaceText).ReadAll()
strFinalText = Replace(strTextToSearch, strTextToFind, strFileReplaceText)
If you want to write this final text back out to a file then add this code:
如果要将此最终文本写回文件,请添加以下代码:
Const ForWriting = 2
Dim strFileFinalOutput
strFileFinalOutput = "C:\FileFinalOutput.txt"
Set objTextFile = objFSO.OpenTextFile(strFileFinalOutput, ForWriting, True)
objTextFile.Write strFinalText
objTextFile.Close
Set objTextFile = Nothing
This code reads the entire file into memory (.ReadAll) and may experience problems with very large files. In this case, the code can be edited to read/search/replace/write the data line by line.
此代码将整个文件读入内存(.ReadAll),并且可能会遇到非常大的文件问题。在这种情况下,可以编辑代码以逐行读取/搜索/替换/写入数据。
If the text you are searching for is not continuous and all on the same line then the search/replace process is more involded and this code will need additional work to handle it.
如果您要搜索的文本不是连续的并且所有文本都在同一行上,那么搜索/替换过程就会更加复杂,并且此代码需要额外的工作来处理它。
#1
4
filea.txt: hello cruel world
filea.txt:你好残酷的世界
fileb.txt: cruel
filec.txt: happy
will make sResult = "hello happy world" after the following has executed.
在执行以下操作后,将使sResult =“hello happy world”。
Dim oFSO
Dim sFileAContents
Dim sFileBContents
Dim sFileCContents
Dim sResult
Set oFSO = CreateObject("Scripting.FileSystemObject")
sFileAContents = oFSO.OpenTextFile("c:\filea.txt").ReadAll()
sFileBContents = oFSO.OpenTextFile("c:\fileb.txt").ReadAll()
sFileCContents = oFSO.OpenTextFile("c:\filec.txt").ReadAll()
sResult = Replace(sFileAContents, sFileBContents, "")
#2
0
FileToSearch is the file with the text you want to search for replacement
FileReplaceText is the file containing the replacement text
FileToSearch是包含要搜索替换文本的文件FileReplaceText是包含替换文本的文件
Edit the value of the variable strTextToFind to contain the text you are searching for and replacing
编辑变量strTextToFind的值以包含要搜索和替换的文本
Dim objFSO
Dim strFileToSearch
Dim strFileReplaceText
Dim strTextToFind
Dim strTextToSearch
Dim strTextReplaceText
Dim strFinalText
strFileToSearch = "C:\FileToSearch.txt"
strFileReplaceText = "C:\FileReplaceText.txt"
strTextToFind = "text to search for here"
Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextToSearch = objFSO.OpenTextFile(strFileToSearch).ReadAll()
strFileReplaceText = objFSO.OpenTextFile(strFileReplaceText).ReadAll()
strFinalText = Replace(strTextToSearch, strTextToFind, strFileReplaceText)
If you want to write this final text back out to a file then add this code:
如果要将此最终文本写回文件,请添加以下代码:
Const ForWriting = 2
Dim strFileFinalOutput
strFileFinalOutput = "C:\FileFinalOutput.txt"
Set objTextFile = objFSO.OpenTextFile(strFileFinalOutput, ForWriting, True)
objTextFile.Write strFinalText
objTextFile.Close
Set objTextFile = Nothing
This code reads the entire file into memory (.ReadAll) and may experience problems with very large files. In this case, the code can be edited to read/search/replace/write the data line by line.
此代码将整个文件读入内存(.ReadAll),并且可能会遇到非常大的文件问题。在这种情况下,可以编辑代码以逐行读取/搜索/替换/写入数据。
If the text you are searching for is not continuous and all on the same line then the search/replace process is more involded and this code will need additional work to handle it.
如果您要搜索的文本不是连续的并且所有文本都在同一行上,那么搜索/替换过程就会更加复杂,并且此代码需要额外的工作来处理它。