Page 1 of 1

Weird Problem with echo command

Posted: 12 Apr 2011 05:23
by Silvermaul
Hallo all,

I have a rather simple question :

this works :
@echo on
for /f "delims=" %%g in ('findstr /I /R /C:"^ *.testdata" %file%') do set var=%%g

Output : ... set var=2: <testdata file="someFile.txt"/>

But when I try to do this :

@echo off
for /f "delims=" %%g in ('findstr /I /R /C:"^ *.testdata" %file%') do set var=%%g
echo. %var%

I get this output :

D:\Tools\eclipse\Workspace\testHtml>set var= <testdata file="someFile.txt"/>
The syntax of the command is incorrect.
D:\Tools\eclipse\Workspace\testHtml>echo. <testdata file="someFile.txt"/>

If I remove the echo then the error message dissapears. Any ideas?

Regards

Stefanos

Re: Weird Problem with echo command

Posted: 12 Apr 2011 06:12
by jeb
The result is the expected result in a batch file :)

Because in batch the variables are expanded before the code is executed.
So you got a line

Code: Select all

echo.<testdata file="someFile.txt"/>


So you try to redirect to/from some obscure locations.

To avoid this you can use quotes, or the delayed expansion with **!** instead of **%**

Code: Select all

setlocal EnableDelayedExpansion
echo.!var!


hope it helps
jeb

Re: Weird Problem with echo command

Posted: 12 Apr 2011 06:13
by Silvermaul
Outstanding...It didn't occur to me that the echo would try to redirect the xml stuff :S

Now it works! Thanks a bunch! :)