Solved Batch file not echoing input lines properly

October 6, 2014 at 18:50:50
Specs: Windows 7
Hi, I'm reading an input xml file and it has many comment lines in the form of:

<!-- This is a comment -->

I'm doing some manipulations with the tags with no problem, but when I echo the lines to a new file, my comment lines are missing the ! character - so they show up in the new file as follows:

<-- This is a comment -->

I have no idea why this happening. How do I correct this?

Here is my code:

set inputFile=./infile.xml
set outputFile=./outfile.xml

::preserve blank lines using FINDSTR, assume no line starts with :
for /f "tokens=1* delims=:" %%A in ('type "%inputFile%" ^| findstr /n "^"') do (

... other code here ...

... Howver Comment lines are simply read and echoed back to outputFile

if "%%B"=="" (echo. >>"%outputFile%") else (echo.%%B>>"%outputFile%")



message edited by Iyes

See More: Batch file not echoing input lines properly

Report •

October 7, 2014 at 02:39:58
If your batch issues a setlocal EnableDelayedExpansion statement the character ! is wiped out from echo command since it is restricted to mark dynamic variables. To work around this issue you have to avoid the setlocal statement and dynamic variables, but this leads to some rearrangement of code. It is possible, since e.g. Windows NT/4.0 did not support that facility, but how much work is needed depends on the complexity of your script.

Report •

October 7, 2014 at 08:15:39
Thanks! I now know my problem, but I have a bigger problem.

I have to use the For loop to iterate over the lines of the file. I also have a counter that is expanded at run time. Now if I omit SETLOCAL ENABLEDELAYEDEXPANSION, my XML comment lines are processed just fine, but my counter is not getting incremented. Both the echo of the lines and incrementing the counter need to happen inside the For loop.

Is there a way to increment the counter inside the loop without using !counter! to reference the incremented value?


Report •

October 7, 2014 at 10:31:40
✔ Best Answer
use "call" to call out to a subroutine, like this:

@echo off
set c=10
set k=
for /L %%a in (1,1,10) do (
set /a c-=1
call :xx %%a
goto :eof

echo %c%
set k=%k% %1
echo %k%

Report •

Related Solutions

Ask Question