我正在尝试替换文本文件中的3个文本。我尝试了这个-
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Aron_", "Lori_")
strNewText1 = Replace(strText, "Aron", "Jason") 'Not working
strNewText2 = Replace(strText, "Sketa", "Skicia") 'Not working
Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForWriting)
objFile.WriteLine strNewText
objFile.WriteLine strNewText1 'Not working
objFile.WriteLine strNewText2 'Not working
objFile.Close我不能想出如何做多个replacement.The代码工作完美的单个替换功能,但不超过one...plz帮助
发布于 2013-02-20 13:52:58
您需要对上一个Replace的结果调用Replace
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "Aron_", "Lori_")
strNewText1 = Replace(strNewText, "Aron", "Jason")
strNewText2 = Replace(strNewText1, "Sketa", "Skicia")
Set objFile = objFSO.OpenTextFile("W:\Test.txt", ForWriting)
objFile.WriteLine strNewText2
objFile.Close
实际上,您可以重用单个变量,而不是使用strNewText、strNewText1、strNewText2。
strText = Replace(strText, "Aron_", "Lori_")
strText = Replace(strText, "Aron", "Jason")
strText = Replace(strText, "Sketa", "Skicia")之后的版本:
objFile.WriteLine strText
此外,您可能希望考虑使用正则表达式一次匹配多个值。(在SO上搜索VBScript正则表达式或VBA正则表达式。)
https://stackoverflow.com/questions/14973105
复制相似问题