且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-23 17:48:24

需要对上一个Replace的结果调用Replace:

You need to call Replace on the results of the previous 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


您实际上可以重用单个变量,而不是使用 strNewTextstrNewText1strNewText2.
strText = Replace(strText, "Aron_", "Lori_")
strText = Replace(strText, "Aron", "Jason")
strText = Replace(strText, "Sketa", "Skicia")

及以后:

objFile.WriteLine strText


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