1.

Solve : Please Help! Batch File to find and replace config values urgently required?

Answer»

Quote from: hitman126 on January 20, 2011, 03:58:44 AM

There is however ONE minor problem I've noticed. The OPENING and closing ApplicationSettings tags have three sets
of empty strings appearing in the OUTPUT file, i.e. <applicationSettings>"""" and </applicationSettings>""""
Can't find the cause for it. Any help on this please?

You didn't show the opening and closing ApplicationSettings tags before. Script below contains code to ignore these.

Hey ! Are you sure about the SPACES in the urls?

example...

Quote
"WEBSERVER 1"

They don't look right to me.




Code: [Select]@echo off
setlocal enabledelayedexpansion

set inputfile=MyConfigUpdate.txt
set outputfile=MyConfigUpdate-changed.txt
if exist "%outputfile%" del "%outputfile%"

for /f "delims=" %%A in (%inputfile%) do (
set "string=%%A"
set string=!string:"=$!
set change=1
echo "!string!" | find "applicationSettings">nul && set change=0
for /f "tokens=1-6 delims=$" %%B in ("!string!") do (

set token1=%%B
set token2=%%C
set token3=%%D
set token4=%%E
set token5=%%F
set token6=%%G

REM <arg key="MyApp1Root" value="http://localhost:9005/config"/>
REM I would like to replace the word localhost with "WebServer 1",
echo !token2! | find "MyApp1Root">nul && set token4=!token4:localhost=WebServer 1!

REM <arg key="MyApp2Root" value="http://localhost:9012/config"/>
REM I would like to replace the word localhost with "AppsServer 2"
echo !token2! | find "MyApp2Root">nul && set token4=!token4:localhost=AppsServer 2!

REM <arg key="WWWRoot" value="http://DBServer:9012/config"/>
REM I would like to replace DBServer with "DatabaseServer 3"
echo !token2! | find "WWWRoot">nul && set token4=!token4:DBServer=DatabaseServer 3!
if !change! equ 1 (
echo !token1!"!token2!"!token3!"!token4!"!token5!>>%outputfile%
) else (
echo !token1!!token2!!token3!!token4!!token5!>>%outputfile%"
)

)

)

REM show results
echo %inputfile%
type "%inputfile%"
echo %outputfile%
type "%outputfile%"

Code: [Select]MyConfigUpdate.txt
<applicationSettings>
<add key="MyApp1Root" value="https://localhost:9001" />
<add key="MyApp2Root" value="http://localhost:9012" />
<add key="WWWRoot" value="http://DBServer.myCompany.com" />
<add key="app.MyTelesalesRootUrl" value="http://localhost:9501" />
<add key="app.MaximumCustomerSearchResults" value="100" />
</applicationSettings>

MyConfigUpdate-changed.txt
<applicationSettings>
<add key="MyApp1Root" value="https://WebServer 1:9001" />
<add key="MyApp2Root" value="http://AppsServer 2:9012" />
<add key="WWWRoot" value="http://DatabaseServer 3.myCompany.com" />
<add key="app.MyTelesalesRootUrl" value="http://localhost:9501" />
<add key="app.MaximumCustomerSearchResults" value="100" />
</applicationSettings>


Discussion

No Comment Found