Echo command and environment variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Echo command and environment variable

#1 Post by balubeto » 06 May 2012 01:35

HI

Using the echo command of Windows 7 SP1, how can I insert, in a text file, an environment variable (like %WINDIR%) without that it is first interpreted by the shell?

THANKS

BYE

miskox
Posts: 553
Joined: 28 Jun 2010 03:46

Re: Echo command and environment variable

#2 Post by miskox » 06 May 2012 03:29

Code: Select all

@echo %%windir%%>outputfile.txt


Hope this helps,
Saso

balubeto
Posts: 136
Joined: 08 Dec 2011 12:14

Re: Echo command and environment variable

#3 Post by balubeto » 06 May 2012 03:38

miskox wrote:

Code: Select all

@echo %%windir%%>outputfile.txt


Hope this helps,
Saso


It not works. Why?

THANKS

BYE

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

Re: Echo command and environment variable

#4 Post by jeb » 06 May 2012 04:28

It depends of the context.

If you want do it from a batch file then the solution of miskox works.

But from the cmd shell it doesn't work the same way.

There doesn't exist an escape possibility, but you can use a workaround.

Code: Select all

echo %^windir%>outputfile.txt
or
echo %windir^%>outputfile.txt
or
echo %win^dir%>outputfile.txt


jeb

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Echo command and environment variable

#5 Post by abc0502 » 06 May 2012 05:17

what miskox said is right but remove one of the % like that

echo %windir% >>outputfile.txt

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

Re: Echo command and environment variable

#6 Post by jeb » 06 May 2012 05:35

Not really, it can only work if windir isn't defined.
Try it with

Code: Select all

echo %path%

And it will fail

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Echo command and environment variable

#7 Post by foxidrive » 06 May 2012 05:48

balubeto wrote:
miskox wrote:

Code: Select all

@echo %%windir%%>outputfile.txt



It not works. Why?


If you are using a CMD prompt then please say so, otherwise we will assume you are using batch files, where the command above works fine.

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: Echo command and environment variable

#8 Post by trebor68 » 06 May 2012 09:04

I hope that I understand you.

You will not write the value of the variable %WINDIR% in the file.
You will write %WINDIR% in the file.

Code: Select all

ECHO The windows folder will you find in ^%WINDIR^%.>outputfile.txt

The result is:
    The windows folder will you find in %WINDIR%.

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

Re: Echo command and environment variable

#9 Post by jeb » 07 May 2012 01:58

@trebor68: As I said before, the caret doesn't escape a percent sign, it's only a workaround that will work often, but not always.

A secure way will be

Code: Select all

for %Y in ("%") do echo %~Ywindir%~Y

Post Reply