Weird Problem with echo command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Silvermaul
Posts: 2
Joined: 12 Apr 2011 04:49

Weird Problem with echo command

#1 Post by Silvermaul » 12 Apr 2011 05:23

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Weird Problem with echo command

#2 Post by jeb » 12 Apr 2011 06:12

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

Silvermaul
Posts: 2
Joined: 12 Apr 2011 04:49

Re: Weird Problem with echo command

#3 Post by Silvermaul » 12 Apr 2011 06:13

Outstanding...It didn't occur to me that the echo would try to redirect the xml stuff :S

Now it works! Thanks a bunch! :)

Post Reply