什么是替换文本文件中的文本的简单方法

时间:2016-09-28 15:14:31

标签: windows batch-file vbscript

我想创建一个小C:\Program Files (x86)\thirdparty\thirdparty.xml文件,可以修复安装程序的常见错误。

假设文件<?xml version="1.0" encoding="UTF-8"?> <!--Default XML file created for/by the Redacted Application.--> <Redcated> <ApplicationConfiguration Environment="ProductionDB"> <key name="redacted" value="redacted" /> <key name="redacted" value="redacted" /> <key name="False Connection" value="True" /> 包含以下行:

  

虚假连接:是

如何将其更改为:

  

错误连接:错误

xml顶行的修改版本 我不能给出实际的xml值,因为它们是公司控制的

<key name="False Connection" value="True" />

要更改的值

'Content-Type': 'application/x-www-form-urlencoded'

1 个答案:

答案 0 :(得分:1)

您可以使用Replace()进行此类简单替换,请尝试以下VBScript代码:

sPath = "C:\Program Files (x86)\thirdparty\thirdparty.xml"
sCharset = "UTF-8"

sContent = LoadTextFromFile(sPath, sCharset)
sContent = Replace(sContent, "<key name=""False Connection"" value=""True"" />", "<key name=""False Connection"" value=""False"" />")
SaveTextToFile sContent, sPath, sCharset

Function LoadTextFromFile(sPath, sCharset)
    With CreateObject("ADODB.Stream")
        .Type = 1 ' TypeBinary
        .Open
        .LoadFromFile sPath
        .Position = 0
        .Type = 2 ' adTypeText
        .Charset = sCharset
        LoadTextFromFile = .ReadText
        .Close
    End With
End Function

Sub SaveTextToFile(sContent, sPath, sCharset)
    With CreateObject("ADODB.Stream")
        .Type = 2 ' adTypeText
        .Open
        .Charset = sCharset
        .WriteText sContent
        .Position = 0
        .Type = 1 ' TypeBinary
        .SaveToFile sPath, 2
        .Close
    End With
End Sub