DosTips.com

A Forum all about DOS Batch
It is currently 30 Jun 2016 15:06

All times are UTC-06:00




Post new topic  Reply to topic  [ 9 posts ] 
Author Message
PostPosted: 06 May 2012 01:35 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
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


Top
   
PostPosted: 06 May 2012 03:29 
Offline

Joined: 28 Jun 2010 03:46
Posts: 250
Code:
@echo %%windir%%>outputfile.txt


Hope this helps,
Saso


Top
   
PostPosted: 06 May 2012 03:38 
Offline

Joined: 08 Dec 2011 12:14
Posts: 61
miskox wrote:
Code:
@echo %%windir%%>outputfile.txt


Hope this helps,
Saso


It not works. Why?

THANKS

BYE


Top
   
PostPosted: 06 May 2012 04:28 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 787
Location: Germany
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


Top
   
PostPosted: 06 May 2012 05:17 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
what miskox said is right but remove one of the % like that

Quote:
echo %windir% >>outputfile.txt


Top
   
PostPosted: 06 May 2012 05:35 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 787
Location: Germany
Not really, it can only work if windir isn't defined.
Try it with
Code:
echo %path%

And it will fail


Top
   
PostPosted: 06 May 2012 05:48 
Offline
Expert

Joined: 10 Feb 2012 02:20
Posts: 5700
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.


Top
   
PostPosted: 06 May 2012 09:04 
Offline

Joined: 01 Jul 2011 08:47
Posts: 143
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%.


Top
   
PostPosted: 07 May 2012 01:58 
Offline
Expert

Joined: 30 Aug 2007 08:05
Posts: 787
Location: Germany
@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


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 9 posts ] 

All times are UTC-06:00


Who is online

Users browsing this forum: Bing [Bot] and 8 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® Forum Software © phpBB Limited