Page 1 of 1

Mystery: Carets

Posted: 08 Sep 2010 03:33
by jeb
Can someone explain this? :)

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "^=one"
set "^^=two"
set "^"
set "caret=^"
call echo I have %%!caret!%% carets, but why?


You got

Code: Select all

^=one
^^=two
I have two carets, but why?


make a guess ...


jeb

Re: Mystery: Carets

Posted: 08 Sep 2010 05:20
by orange_batch
jeb wrote:

Code: Select all

I have two carets, but why?


Because you touch yourself at night. :lol:

Are you asking for yourself, or to see if anyone knows? Either way, the command processor seems to double up the "string" caret before execution. No way around this...

Re: Mystery: Carets

Posted: 08 Sep 2010 08:54
by jeb
I know the answer. :wink:

You are near, but as you can see the solution is not so simple.

Code: Select all

setlocal EnableDelayedExpansion
set varE=^^!varE^^!#
set varP=%%varP%%#
set "caret=^"
call echo !caret!


You got only one ^

Re: Mystery: Carets

Posted: 08 Sep 2010 14:34
by miskox
Let me try it.

I will use BASIC, maybe in this way it will be easier to understand:

10 set ^="one"
20 set ^^="two"
30 set ^="" : rem Deletes variable ^
40 set caret=^ : rem variable caret gets the value of variable ^
50 print "..."

In line 50 variable from line 20 gets processed which is single caret because it is escaped by another caret.

Am I right?

Saso

Re: Mystery: Carets

Posted: 08 Sep 2010 16:40
by aGerman
!caret! is expanded to ^. The CALL command doubles the ^. So you have %^^% and this is expandet to two.
_____________________________________________________________________________

jeb wrote:You got only one ^

Hmm, I think you got
echo ^^
and this results to a single caret.

Regards
aGerman

Re: Mystery: Carets

Posted: 08 Sep 2010 19:03
by ghostmachine4
that's one of the problem with DOS. Caret (or other special characters ) should not be allowed to be used as variable names.

Re: Mystery: Carets

Posted: 09 Sep 2010 00:35
by jeb
aGerman wrote:!caret! is expanded to ^. The CALL command doubles the ^. So you have %^^% and this is expandet to two.


Yes this it is, but ...

Code: Select all

echo ^ zero
echo ^^ one caret
call echo !caret! one caret
call echo ^^ one caret
call echo ^^^^ two carets "^^^^" four carets
rem BUT NOW
call echo ^^^^ two carets "^^^^" eight carets (oho)
call call echo ^^^^ two carets "^^^^" sixteen carets (oh oh)


Each call doubles all carets in a line, also in quotes.
Normally, the carets are escaping themself, so a double caret is shrinking to one, but this does not work in quotes.
And as the escape-phase is after the % expanding phase you get the result of %%!caret!%% as
%^^%

Re: Mystery: Carets

Posted: 09 Sep 2010 12:19
by orange_batch
Haha jeb, a real DOS scientist. Bravo.