"%systemroot%" chages to "C:\windows"

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

"%systemroot%" chages to "C:\windows"

#1 Post by Mohammad_Dos » 21 Aug 2011 07:43

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?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

Re: "%systemroot%" chages to "C:\windows"

#2 Post by !k » 21 Aug 2011 09:06

double %%

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: "%systemroot%" chages to "C:\windows"

#3 Post by Mohammad_Dos » 21 Aug 2011 19:00

did not work :cry:

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: "%systemroot%" chages to "C:\windows"

#4 Post by dbenham » 21 Aug 2011 19:19

from the command line:

Code: Select all

echo ^%systemroot^%

from within a batch file:

Code: Select all

echo %%systemroot%%



Dave Benham

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: "%systemroot%" chages to "C:\windows"

#5 Post by phillid » 23 Aug 2011 00:45

Wouldn't one have to use the '>' or the '>>' symbols as well to put into the file?

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

Re: "%systemroot%" chages to "C:\windows"

#6 Post by jeb » 24 Aug 2011 16:16

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: "%systemroot%" chages to "C:\windows"

#7 Post by dbenham » 24 Aug 2011 16:49

Wow - I had no idea I had stumbled on a hack. :lol:

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

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

Re: "%systemroot%" chages to "C:\windows"

#8 Post by jeb » 24 Aug 2011 17:01

dbenham wrote:Wow - I had no idea I had stumbled on a hack. :lol:

You are not alone :)
Even Microsoft didn't know it ...
Even the help at reg add /? recommends the hack.

jeb

Mohammad_Dos
Posts: 84
Joined: 08 Sep 2010 10:25
Location: Iran,Kashan
Contact:

Re: "%systemroot%" chages to "C:\windows"

#9 Post by Mohammad_Dos » 29 Aug 2011 08:46

what about this?

Code: Select all

%%a

changes to:

Code: Select all

%a

Post Reply