Option Explicit
Dim strDstFile, strTmpFile, objFso, objSrcFile, objDstFile, strRead, strDstChr, objReg
strDstChr = "127.0.0.1" '"host=XXXX"替换后的目标字串
strDstFile = "c:\123.txt" '需要更改内容的文件完整路径,请自行更改
strTmpFile = "c:\tmp.txt"
Set objFso = CreateObject("Scripting.FileSystemObject")
Set objReg = New RegExp
With objReg
.Global = False
.IgnoreCase = True
.Pattern = "^(.*host\s*=\s*).+$"
End With
Set objSrcFile = objFso.OpenTextFile(strDstFile, 1)
Set objDstFile = objFso.OpenTextFile(strTmpFile, 2, True)
Dim strRslt
strRslt = ""
Do Until objSrcFile.AtEndOfStream
strRead = objSrcFile.ReadLine
If objReg.Test(strRead) = True Then
strRead = objReg.Replace(strRead, "$1" & strDstChr)
End If
strRslt = strRslt & strRead & vbCrLf
Loop
objSrcFile.Close
objDstFile.Write strRslt
objDstFile.Close
objFso.CopyFile strTmpFile, strDstFile, True
objFso.DeleteFile strTmpFile, True
Set objSrcFile = Nothing
Set objDstFile = Nothing
Set objFso = Nothing
Set objReg = Nothing
'建议用 VBS 来完成,因为 VBS 的正则比批处理更智能...