You are not logged in.
Pages: 1
Hi,
I would like to ask you for help.
I have input file like this:
<cfg key="WaitingTime" value="00:05:00"/>
<cfg key="ConnectionTimeout" value="0"/>
<cfg key="ConnectionRetry" value="false"/>
<cfg key="ConnectionClose" value="true"/>
<cfg key="CachePath" value="%TEMP%\conn\"/>
<!-- "c:\documents and settings\" -->
<!-- <ref ref="Logging" /> -->
- Betwwen the lines can be empty lines and there might be also not empty lines, but only with tabs and spaces. Also XML lines may be indented.
In this configuration file I would like to change the "false" into "true" only on the line ConnectionRetry.
- I get the inspiration on Dostips.COM and modified script to
@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL enableDELAYEDEXPANSION
for /f "tokens=1,* delims=]" %%A in ('find /n /v "" "sample.txt"') do (
set "line=%%B"
if defined line (
set "dummy=!line:ConnectionRetry=!"
if !dummy!==!line! (set "line=echo.!line!") else (set "line=echo.!line:false=true!")
for /f "delims=" %%X in ('"echo."!line!""') do %%~X
rem pause
) ELSE echo.
)
But the problem is that XML comments <!-- are broken. The exlamation mark makes it broken.
- I have tried to keep disableDELAYEDEXPANSION, but then I'm not able to evaluate condition inside FOR loop.
Thanks for any ideas
Offline
Hi,
the solution got a little longer than the previous. I have chosen different aproach to split the file into 3 pieces and then join them together.
@echo off
Setlocal EnableDelayedExpansion
set ModifyConnectionRetry=true
REM Everything is in IF, because default value in config is false. Nothing needs to be done for false. For true modify the file.
IF "%ModifyConnectionRetry%"=="true" (
REM get sample.txt into temp location
IF DEFINED ProgramFiles^(x86^) (
REM 64bit
copy /y "sample.txt" "%temp%\tmpfile.original"
) ELSE (
REM 32bit
copy /y "sample.txt" "%temp%\tmpfile.original"
)
REM get line number where is located ConnectionRetry
FOR /F "tokens=1,* delims=:" %%a IN ('findstr /n "ConnectionRetry" "%temp%\tmpfile.original"') DO (set "location_line=%%a")
REM get 3rd part of file - everything after line containing ConnectionRetry
more +!location_line! "%temp%\tmpfile.original">"%temp%\tmpfile-C.part"
REM get 2nd part of file - line containing ConnectionRetry and replace text
set /a "location_line-=1"
more +!location_line! "%temp%\tmpfile.original">"%temp%\tmpfile-B.part"
set /p text_replace=<"%temp%\tmpfile-B.part"
set "text_replace=!text_replace:false=%ModifyConnectionRetry%!"
echo !text_replace!>"%temp%\tmpfile-B.part"
REM get 1st part of file - everything before line containing ConnectionRetry
set /a "location_line-=1"
FOR /L %%a IN (0,1,!location_line!) DO (
more +%%a "%temp%\tmpfile.original">"%temp%\tmpfile-X.tmp"
set /p text_line=<"%temp%\tmpfile-X.tmp"
IF NOT ERRORLEVEL 1 (echo !text_line!>>"%temp%\tmpfile-A.part") ELSE (echo.>>"%temp%\tmpfile-A.part")
)
REM merge 3 parts into single file and replace original sample.txt
copy /b /y "%temp%\tmpfile-?.part" "%temp%\tmpfile-result.replace"
REM replace file.cfg in correct location
IF DEFINED ProgramFiles^(x86^) (
REM 64bit
ren "sample.txt" "sample.original"
copy /y "%temp%\tmpfile-result.replace" "sample.txt"
) ELSE (
REM 32bit
ren "sample.txt" "sample.original"
copy /y "%temp%\tmpfile-result.replace" "sample.txt"
)
REM cleanup temporary files
del "%temp%\tmpfile.original" > nul
del "%temp%\tmpfile-result.replace" > nul
del "%temp%\tmpfile-?.part" > nul
del "%temp%\tmpfile-?.tmp" > nul
)
Offline
You may also use this method:
@echo off
setlocal EnableDelayedExpansion
rem Get line number where ConnectionRetry is located
for /F "delims=:" %%a in ('findstr /N "ConnectionRetry" "%temp%\tmpfile.original"') do set "location_line=%%a"
rem Block used to read-input-file/create-output-file
< "%temp%\tmpfile.original" (
rem Read the first line from input file
set /P "line="
rem Copy lines up to the replace line
for /L %%i in (2,1,%location_line%) do set /P "line=!line!" & echo/
rem Output the line with the desired value replaced
echo !line:false=true!
rem Copy the rest of lines
findstr "^"
) > "%temp%\tmpfile-result.replace"
However, this method preserve leading spaces in the lines just in Windows XP. After that version SET /P command was changed, so it remove leading spaces...
You may also review this example and the another one posted by me at that topic. Further details at this post.
Last edited by Aacini (22 Mar 2015 01:14)
Offline
Pages: 1