Page 1 of 1
Echo command and environment variable
Posted: 06 May 2012 01:35
by balubeto
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
Re: Echo command and environment variable
Posted: 06 May 2012 03:29
by miskox
Re: Echo command and environment variable
Posted: 06 May 2012 03:38
by balubeto
miskox wrote:Hope this helps,
Saso
It not works. Why?
THANKS
BYE
Re: Echo command and environment variable
Posted: 06 May 2012 04:28
by jeb
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
Re: Echo command and environment variable
Posted: 06 May 2012 05:17
by abc0502
what miskox said is right but remove one of the % like that
echo %windir% >>outputfile.txt
Re: Echo command and environment variable
Posted: 06 May 2012 05:35
by jeb
Not really, it can only work if windir isn't defined.
Try it with
And it will fail
Re: Echo command and environment variable
Posted: 06 May 2012 05:48
by foxidrive
balubeto wrote: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.
Re: Echo command and environment variable
Posted: 06 May 2012 09:04
by trebor68
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%.
Re: Echo command and environment variable
Posted: 07 May 2012 01:58
by jeb
@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