Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Please Help! Batch File to find and replace config values urgently required  (Read 9267 times)

0 Members and 1 Guest are viewing this topic.

dbraju_vizag

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Windows 7
    Hi,
    I have what should hopefully be a very simple scenario.
    i need a batch scipt to replace the following text:
    For e.g, if the web config file has the following keys:
    <add key="Username" value="TRNG"/>
    I would like to replace the word TRNG with "USER",
    For:
    <add key="Password" value="zodiacxx"/>
    I would like to replace the word Zodiacxx with "123abc",
    for:
    <add key="msgServerhost" value="10.200.49.146"/>
    I would like to replace the value 10.200.49.146 with "10.200.85.58",
    i have attaches the below file.
    Any ideas how I can achieve this please? I believe the For loop is my best option but the correct syntax is somehow stumped me.

    Thanks.

    PS: My script can only be a batch file and no other file type or third-party application.

    [year+ old attachment deleted by admin]

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Is WSH/VBS available on your PC?  That is a default capability in PC's from XP and onward.

    Do you want to treat both of these the same way?

        <add key="Username" value="trng" />
        <add key="Username" value="TRNG"/>

    dbraju_vizag

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Windows 7
       treat  as -
      <add key="Username" value="TRNG"/>

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      There was another question above.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      Processing XML files with batch code can be problematic with all the special characters. With your Win7 machine, I'm curious why you didn't try a Powershell or VBScript solution. But no matter, we aim to please.

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion

      for /f "delims=>" %%v in (webconfig.txt) do (
        set line=%%v
        set line=!line:trng=USER!
        set line=!line:zodiacxx=123abc!
        set line=!line:10.200.49.146=10.200.85.58!
        echo !line!^> >> newWebConfig.xml
      )

      The code gets over enthusiastic with the comment lines, so you may want to tweak the code accordingly.  8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      The above solution had more problems with the comments than I originally noticed. In fact, it broke the XML format.

      This new and improved solution is technically a batch file and has a BAT extension to prove it, but takes advantage of other tools available on your system. I'm not a big fan of these hybrid files, but they do produce results and JScript has advantages over VBScript by not requiring an intermediate work file and not requiring extensive use of escape characters.

      Code: [Select]
      @set @JScript=1/*
      @echo off
      setlocal

      cscript //nologo //E:JScript %~f0
      goto :eof

      */

      // JScript
      //
      var ForReading= 1
      var ForWriting = 2

      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var input = fso.OpenTextFile("c:\\temp\\webconfig.txt", ForReading)
      var output = fso.OpenTextFile("c:\\temp\\newWebConfig.xml", ForWriting, true)

      var data = input.ReadAll()
      data = data.replace("TRNG", "USER")
      data = data.replace("zodiacxx", "123abc")
      data = data.replace("10.200.49.146", "10.200.85.58")

      output.Write(data)

      input.Close()
      output.Close()

      Save the script with a BAT extension and run as any other batch file in the command prompt window.  8)
      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      It's a neat solution.  One issue, it only changes the first occurrence of the keywords in the file.

      Sidewinder



        Guru

        Thanked: 139
      • Experience: Familiar
      • OS: Windows 10
      It's a neat solution.  One issue, it only changes the first occurrence of the keywords in the file.

      Two issues actually. A regular expression is more helpful here. Tweaks include a regular expression for global, case insensitive replace function.

      Code: [Select]
      @set @JScript=1/*
      @echo off
      setlocal

      cscript //nologo //E:JScript %~f0
      goto :eof

      */

      // JScript
      //
      var ForReading= 1
      var ForWriting = 2

      var fso = new ActiveXObject("Scripting.FileSystemObject");
      var input = fso.OpenTextFile("c:\\temp\\webconfig.txt", ForReading)
      var output = fso.OpenTextFile("c:\\temp\\newWebConfig.xml", ForWriting, true)

      var data = input.Readall()
      data = data.replace(/TRNG/gi, "USER")
      data = data.replace(/zodiacxx/gi, "123abc")
      data = data.replace(/10.200.49.146/g, "10.200.85.58")

      output.Write(data)

      input.Close()
      output.Close()

      The true sign of intelligence is not knowledge but imagination.

      -- Albert Einstein

      foxidrive



        Specialist
      • Thanked: 268
      • Experience: Experienced
      • OS: Windows 8
      Thanks for the code base for future use.

      I added some context to the search and replace so that false matches are minimised, and allowed for long filenames in the script name.


      Code: [Select]
      @set @JScript=1/*
      @echo off
      setlocal

      cscript //nologo //E:JScript "%~f0"
      goto :eof

      */

      // JScript
      //
      var ForReading= 1
      var ForWriting = 2

      var fso = new ActiveXObject("Scripting.FileSystemObject");
      // var input = fso.OpenTextFile("c:\\temp\\webconfig.txt", ForReading)
      // var output = fso.OpenTextFile("c:\\temp\\newWebConfig.xml", ForWriting, true)
      var input = fso.OpenTextFile("webconfig.txt", ForReading)
      var output = fso.OpenTextFile("newWebConfig.xml", ForWriting, true)


      var data = input.Readall()
      data = data.replace(/value=\x22TRNG\x22/gi, "value=\x22USER\x22")
      data = data.replace(/value=\x22zodiacxx\x22/gi, "value=\x22123abc\x22")
      data = data.replace(/value=\x2210.200.49.146\x22/g, "value=\x2210.200.85.58\x22")

      output.Write(data)

      input.Close()
      output.Close()

      dbraju_vizag

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Windows 7
        Thank you