如何用批处理结合VBS实现批量替换修改txt文本???

我这D:\1\目录里有很多TXT文档,1.txt,2.txt,3.txt.........
里面有2处内容需要替换:
“name=1”替换成“name=19”,
还有“path=e:game”替换成“path=e:\game”
因为bat很难实现对txt的替换,我这有2个VBS分别替换2处,要运行2个VBS才可以替换成功1个txt文本,问题是运行1次bat不可以打开2个vbs,而且郁闷的是要指定路径D:\1\1.txt,这样每次要修改,有没有可以结合bat和vbs同时批量替换2处的,谢谢!下面是我的代码:

Set FSO = Nothing
Dim strFile: strFile = "D:\1\1.txt"
Dim FSO: Set FSO = CreateObject("Scripting.FileSystemObject")
Dim objFile: Set objFile = FSO.OpenTextFile(strFile)
Dim strContent: strContent = objFile.Readall
objFile.Close

Dim objRegEx: Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.IgnoreCase = True
objRegEx.Pattern = "path=e:game"
Dim objNewText: objNewText = objRegEx.Replace(strContent,"path=e:\game")

Dim objTextFile: Set objTextFile = FSO.CreateTextFile("D:\1\1.txt")
objTextFile.Write objNewText
objTextFile.Close

Set FSO = Nothing

突然来了点灵感,给你来一行另类的。
注意,代码只有一行。。

批处理 放在txt文件夹下运行

@echo off& for %%1 in (*.txt)do mshta vbscript:createobject("scripting.filesystemobject").opentextfile("wind-%%~1",2,true).writeline(replace(replace(createobject("scripting.filesystemobject").opentextfile("%%~1",1).readall,"name=1","name=19"),"path=e:game","path=e:\game"))(window.close)&& move "wind-%%~1" "%%~1"
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-11-22
思路,你看一下你下面大概有多少个文件,如果是以数字开头的那么,最大的TXT编号是多少,那么你在执行替换之前 用
for i=1 to 最大值
用过i来自动替换文件名,在通过FSO对文件是否纯在做个判断
如果存在就执行2此替换
如果不错在就不执行
next
相似回答