0

I'm trying to replace a string in one text file but that string contains some special characters (=,"") inside so I'm unable to replace the string as expected.

I've written following batch command and it works well with normal string (like "1234", "nanana") but not with the string I'm trying. I tried caret also to escape special characters but no luck.

Can anybody help me out with this as I'm new to batch.

@echo off 
    setlocal enableextensions disabledelayedexpansion

    set "search=<add key="Domain" value="001" />"
    set "replace=<add key="Domain" value="001" />"

    set "textFile=C:\Signing\SigningTool\Input.txt"

    for /f "delims=" %%i in ('type "%textFile%" ^& break ^> "%textFile%" ') do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        >>"%textFile%" echo(!line:%search%=%replace%!
        endlocal
    )

the exact string I want to replace is <add key="Domain" value="001" /> with <add key="Domain" value="000" />

Mayank Tripathi
  • 809
  • 1
  • 7
  • 11
  • Use a different scripting language, not one designed for simply executing a series of basic commands, perhaps [tag:wsh] or [tag:powershell], which are part of most modern [tag:windows] Operarting Systems. – Compo Mar 18 '20 at 13:11
  • Perhaps write to new file and use `findstr` to get the line number containing the string to replace into a variable `lineNum`, then using a counter in loop write up to that line number, echo your new string, then read again but use `"skip=!lineNum!"` in your `for` loop to skip already written lines. – Durry42 Mar 18 '20 at 13:50

0 Answers0