使用 vbscript 替换文本文件中的多个文本 [英] Replace multiple text in a text file using vbscript
本文介绍了使用 vbscript 替换文本文件中的多个文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从文本文件中替换 3 个文本.我试过这个-
I am trying to replace 3 text from a text file.I tried this-
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
我无法弄清楚如何进行多次替换.代码对于单个替换功能来说很完美,但不能超过一个...请帮忙
i am not able to figure out how to do multiple replacement.The code working perfect for single replace function but not more than one...plz help
推荐答案
需要对上一个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
您实际上可以重用单个变量,而不是使用
strNewText
、strNewText1
、strNewText2
.
strText = Replace(strText, "Aron_", "Lori_")
strText = Replace(strText, "Aron", "Jason")
strText = Replace(strText, "Sketa", "Skicia")
及以后:
objFile.WriteLine strText
此外,您可能需要考虑使用正则表达式一次匹配多个值.(在 SO 上搜索 VBScript 正则表达式 或 VBA 正则表达式.)
这篇关于使用 vbscript 替换文本文件中的多个文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文