View unanswered posts | View active topics It is currently 25 Jul 2014 07:39



Post new topic Reply to topic  [ 9 posts ] 
Echo command and environment variable 
Author Message

Joined: 08 Dec 2011 12:14
Posts: 20
Post Echo command and environment variable
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


06 May 2012 01:35
Profile

Joined: 28 Jun 2010 03:46
Posts: 163
Post Re: Echo command and environment variable
Code:
@echo %%windir%%>outputfile.txt


Hope this helps,
Saso


06 May 2012 03:29
Profile

Joined: 08 Dec 2011 12:14
Posts: 20
Post Re: Echo command and environment variable
miskox wrote:
Code:
@echo %%windir%%>outputfile.txt


Hope this helps,
Saso


It not works. Why?

THANKS

BYE


06 May 2012 03:38
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 666
Location: Germany
Post Re: Echo command and environment variable
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:
echo %^windir%>outputfile.txt
or
echo %windir^%>outputfile.txt
or
echo %win^dir%>outputfile.txt


jeb


06 May 2012 04:28
Profile

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
Post Re: Echo command and environment variable
what miskox said is right but remove one of the % like that

Quote:
echo %windir% >>outputfile.txt


06 May 2012 05:17
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 666
Location: Germany
Post Re: Echo command and environment variable
Not really, it can only work if windir isn't defined.
Try it with
Code:
echo %path%

And it will fail


06 May 2012 05:35
Profile
Online

Joined: 10 Feb 2012 02:20
Posts: 4015
Post Re: Echo command and environment variable
balubeto wrote:
miskox wrote:
Code:
@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.


06 May 2012 05:48
Profile

Joined: 01 Jul 2011 08:47
Posts: 110
Post Re: Echo command and environment variable
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:
ECHO The windows folder will you find in ^%WINDIR^%.>outputfile.txt

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


06 May 2012 09:04
Profile
Expert

Joined: 30 Aug 2007 08:05
Posts: 666
Location: Germany
Post Re: Echo command and environment variable
@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:
for %Y in ("%") do echo %~Ywindir%~Y


07 May 2012 01:58
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 


Who is online

Users browsing this forum: Bing [Bot] and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.