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

Pra*_*nks 2 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(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
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何进行多次更换.该代码适用于单个替换功能但不超过一个... plz帮助

Zev*_*itz 7

你需要调用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
Run Code Online (Sandbox Code Playgroud)


其实你可以再使用一个变量,而不是有strNewText,strNewText1,strNewText2.

strText = Replace(strText, "Aron_", "Lori_")
strText = Replace(strText, "Aron", "Jason")
strText = Replace(strText, "Sketa", "Skicia")
Run Code Online (Sandbox Code Playgroud)

然后:

objFile.WriteLine strText
Run Code Online (Sandbox Code Playgroud)


此外,您可能需要考虑使用正则表达式一次匹配多个值.(在SO上搜索VBScript正则表达式VBA正则表达式.)