Home > database >  Batch file not replacing string if it contains spaces
Batch file not replacing string if it contains spaces

Time:04-02

I'm using this script to replace string (containing spaces) in file (it's working if variables doesn't have spaces)

Script:

for /f "tokens=*" %%i in (File.txt) do (
    for /f "tokens=1,2 delims=<>" %%a in ("%%i") do (
        if %%a == Salutation (
            powershell -Command "(gc File.txt) -replace '%%b', 'bye' | Out-File -encoding ASCII File.txt"
        )
        if %%a == path (
            powershell -Command "(gc File.txt) -replace '%%b', 'C:\Program File\It worked' | Out-File -encoding ASCII File.txt"
        )
    )
)
pause

File:

<Salutation>hello</Salutation>
<try>this</try>
<path>C:\Program File\Try Spaces\This</path>

Output:

enter image description here

CodePudding user response:

You can use .replace() so there's no need to worry about regex expressions.

@echo off
rem replace.bat

for /f "tokens=*" %%i in (File.txt) do (
    for /f "tokens=1,2 delims=<>" %%a in ("%%i") do (
        if %%a == Salutation (
            powershell -Command "(gc File.txt) -replace '%%b', 'bye'"
        )
        if %%a == path (
            powershell -Command "(gc File.txt).replace('%%b', 'C:\Program File\It worked')"
        )
    )
)
.\replace.bat

<Salutation>bye</Salutation>
<try>this</try>
<path>C:\Program File\Try Spaces\This</path>
<Salutation>hello</Salutation>
<try>this</try>
<path>C:\Program File\It worked</path>

Another powershell way. The xml is invalid without a top node.

<doc>
  <Salutation>hello</Salutation>
  <try>this</try>
  <path>C:\Program File\Try Spaces\This</path>
</doc>
[xml]$xml = get-content file.txt
$xml.doc.Salutation = 'bye'
$xml.doc.path = 'C:\Program File\It worked'
$xml.save("$pwd\file.txt")

CodePudding user response:

Here is a batch-file solution without the use of Powershell.

@echo off
set "search=Salutation"
set "search2=path"
set "replace=replaced"
set "replace2=C:\Program Files\It Worked"
set "File=File.txt"

for /f "delims=" %%i in ('type "%File%" ^& break ^> "%File%" ') do (
    setlocal enabledelayedexpansion
    set "line=%%i"
    for /f "tokens=1-4*delims=><" %%a in ("!line!") do (
         if "%%a" == "%search%" set "line=!line:%%b=%replace%!"
         if "%%b" == "%search%" set "line=!line:%%c=%replace%!"
         if "%%a" == "%search2%" set "line=!line:%%b=%replace2%!"
         if "%%b" == "%search2%" set "line=!line:%%c=%replace2%!"
    )
    >>"%File%" echo(!line!
    endlocal
)

Unfortunately this now has four if statements (two for each search/replace) because of your example input. Typically xml format is indented i.e:

<tag>
    <Salutation>hello</Salutation>
    <try>this</try>
    <path>C:\Program File\Try Spaces\This</path>
</tag>

but that will depend on how the file was saved and how it is read. So the code allows for indented tags as well. If however the file content remains the same for either, you can simply remove the irrelevant if statements.

  • Related