使用vbscript替换文本文件中的多个文本

使用vbscript替换文本文件中的多个文本,vbscript,replace,text-files,Vbscript,Replace,Text Files,我正在尝试替换文本文件中的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(s

我正在尝试替换文本文件中的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

我不知道如何进行多次替换。该代码非常适合于单次替换功能,但不超过一次…plz帮助

您需要根据上一次的结果调用
替换

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


此外,您可能需要考虑使用正则表达式来同时匹配多个值。(搜索VBScript正则表达式或VBA正则表达式)

我试过-strNewText=Replace(strText,“Aron”,“Lori”)strNewText=Replace(strNewText,“Aron”,“Jason”)。但它不起作用。什么不起作用?您是否收到错误,或者文件没有写入,或者文件的文本没有更改?仅在第一次替换时,文件正在写入,文本正在更改,但在第二次替换时,文本没有更改..刚刚看到您编辑的部分,现在使用单变量运行良好…感谢您的及时回复和帮助!!