-1

I want to create a small vbs/bat file that can fix a common bug with a program on it's installation.

Say the file C:\Program Files (x86)\thirdparty\thirdparty.xml contains the line:

False connection: True

How can I change that to:

False Connection: False


A modified version of the xml top lines I cannot give the actual xml values as they are company controlled

<?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" />

Value to be changed

<key name="False Connection" value="True" />
user692942
  • 14,779
  • 6
  • 66
  • 157
Dansmith
  • 150
  • 9
  • 1
    You may find this useful: http://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – Ryan Sep 28 '16 at 15:17
  • 2
    As I said in your previous question that you deleted, you could use a VBscript with XMLDOM. But we would need to see the layout of the XML file regardless of what language we use to replace the text. – Squashman Sep 28 '16 at 15:22
  • I have added the layout – Dansmith Sep 28 '16 at 15:29
  • 2
    I would suggest once again that you remove the `batch-file` tag and stick to a scripting type which properly supports making alterations to unicode files. perhaps sticking with the vbscript method which you originally provided a script for and have now removed. – Compo Sep 28 '16 at 15:36
  • @compo I couldn't get it to work, the link is http://stackoverflow.com/questions/1115508/batch-find-and-edit-lines-in-txt-file – Dansmith Sep 28 '16 at 15:52
  • 1
    Here is a great tutorial on creating, modify and writing to XML files with Vbscript from Microsoft. https://technet.microsoft.com/en-us/magazine/2008.02.heyscriptingguy – Squashman Sep 28 '16 at 16:06

1 Answers1

1

You can just use Replace() for such simple replacements, try the below VBScript code:

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
omegastripes
  • 11,612
  • 3
  • 35
  • 77
  • I thank you for your answer, however it does not seem to be doing anything. – Dansmith Sep 29 '16 at 12:18
  • although I resolved it in a seperate way using xcopy to copy a working copy of the xml to the location, I'll give you the Answer + – Dansmith Sep 29 '16 at 15:45