Page 1 of 1
"%systemroot%" chages to "C:\windows"
Posted: 21 Aug 2011 07:43
by Mohammad_Dos
I want to save "%systemroot" in C:\copy\jay.bat with batch file
i use it:
echo "%systemroot%" C:\copy\jay.bat"
but changes to C:\WINDOWS
what should i do?
Re: "%systemroot%" chages to "C:\windows"
Posted: 21 Aug 2011 09:06
by !k
double %%
Re: "%systemroot%" chages to "C:\windows"
Posted: 21 Aug 2011 19:00
by Mohammad_Dos
did not work

Re: "%systemroot%" chages to "C:\windows"
Posted: 21 Aug 2011 19:19
by dbenham
from the command line:
from within a batch file:
Dave Benham
Re: "%systemroot%" chages to "C:\windows"
Posted: 23 Aug 2011 00:45
by phillid
Wouldn't one have to use the '>' or the '>>' symbols as well to put into the file?
Re: "%systemroot%" chages to "C:\windows"
Posted: 24 Aug 2011 16:16
by jeb
dbenham wrote:from the command line:
Code:
echo ^%systemroot^%
from within a batch file:
Code:
echo %%systemroot%%
Within a batch file it's correct.
But from the command line it's only a hack, it isn't bullet proof,
as the caret doesn't escape a percent.
In reallity it only avoids the expansion.
Btw. the first caret is completly useless.
The second caret builds only an undefined variable name, and undefined variables stay unchanged on the command line.
But the carets are removed in the speacial character phase later so the result is
%systemroot%And the trick fails if there is a variable named systemroot^
Code: Select all
set "systemroot^=fail"
echo ^%systemroot^%
Output wrote:fail
I didn't see a single command solution, only with a helper variable.
Code: Select all
set "helper=systemroot"
echo %%helper%%
jeb
Re: "%systemroot%" chages to "C:\windows"
Posted: 24 Aug 2011 16:49
by dbenham
Wow - I had no idea I had stumbled on a hack.
I knew double percents don't work on the command line like they do in batch mode, and I was curious how to get the result on command line. "Escaping" the percent was the 1st thing I tried and it appeared to work.
It seems like a pretty good hack. I don't recall seeing ^ in a variable name very often.
Excellent explanation of how it actually works. Thanks jeb.
Based on jeb's explanation I realize the hack works if you put the ^ anywhere between the percents (as long as no variable is named that way):
Code: Select all
echo %^systemroot%
echo %system^root%
etc.
Dave Benham
Re: "%systemroot%" chages to "C:\windows"
Posted: 24 Aug 2011 17:01
by jeb
dbenham wrote:Wow - I had no idea I had stumbled on a hack.
You are not alone
Even Microsoft didn't know it ...
Even the help at
reg add /? recommends the hack.
jeb
Re: "%systemroot%" chages to "C:\windows"
Posted: 29 Aug 2011 08:46
by Mohammad_Dos
what about this?
changes to: