Page 1 of 1

Weird expansion - %:text% and %stripped:text%

Posted: 30 Jun 2011 10:21
by dbenham
Can anyone explain this :?:
My guess is I'll hear from jeb with an "obvious" parsing explanation :wink: :lol:

Code: Select all

@echo off
cls
setlocal disableDelayedExpansion
set :
echo %:Percent expansion is ignored if leading character is a colon%
echo %:Why?%
echo %=Any text:Text through first : is stripped if lead is not :%
setlocal enableDelayedExpansion
echo !:The same is true with delayed expansion!
echo !=Any text:And again%
endlocal
echo Variable name containing : cannot be expanded!
set ":Hello=Goodbye"
set "Hello:World=Goodbye again"
set :
set Hello
echo %:Hello%
echo %Hello:World%
setlocal enableDelayedExpansion
echo !:Hello!
echo !Hello:World!

Output:

Code: Select all

Environment variable : not defined
:Percent expansion is ignored if leading character is a colon
:Why?
Text through first : is stripped if lead is not :
:The same is true with delayed expansion
And again
Variable name containing : cannot be expanded!
:Hello=Goodbye
Hello:World=Goodbye again
:Hello
World
:Hello
World


Dave Benham

Re: Weird expansion - %:text% and %stripped:text%

Posted: 01 Jul 2011 09:47
by dbenham
Curiouser and curiouser:

Code: Select all

@echo off
cls
setlocal enableDelayedExpansion
set "dbug="
echo(!=This does not display!The bogus variable expands to nothing.
echo(!=:But this does get displayed "because" of the colon!
!=:echo Why doesn't this work?!
!=:And why does this NOT cause an error?!
%=:echo Yet this does work.%
%=:And this DOES causes an error%

Output:

Code: Select all

The bogus variable expands to nothing.
But this does get displayed "because" of the colon
Yet this does work.
'And' is not recognized as an internal or external command,
operable program or batch file.

None of this makes sense to me :?
Is no one else surprised? or does anyone have an explanation :?:

Dave Benham

Re: Weird expansion - %:text% and %stripped:text%

Posted: 01 Jul 2011 12:53
by Ed Dyreen

Code: Select all

%=:@echo off &echo what the  hell ? &pause &exit /b%
:roll: I wouldn't be suprised if it worked:

Code: Select all

C:>%:SESAM ?%
Yes
C:>%:OPEN !%
passw: Big Bill<ENTER>
Acces granted, Secret DOS commands have been activated.
:P

Re: Weird expansion - %:text% and %stripped:text%

Posted: 03 Jul 2011 02:46
by jeb
dbenham wrote:Can anyone explain this :?:
My guess is I'll hear from jeb with an "obvious" parsing explanation :wink: :lol:


You are right, it is obvious as you nearly got the explanation ... 8)
It's the parser who decide when a percent statement ends.

A percent statement begins with a percent (wow), then it searches to the next percent or a colon.
With a percent sign it ends.
If a colon is found, the variable is searched and if it isn't defined the statement ends.
Therefore you you get the rest of the line if a variable isn't defined.

Code: Select all

set "NotDef="
set "var=content"
echo 1 %NotDef%
echo 2 %NotDef:~1%
echo 3 %NotDef%var%
echo 4 %NotDef:~1%var%
echo 5 %NotDef%%
echo 6 %NotDef:~1%%


How to expand variables with a colon inside of the var?
Obviously you can't expand it the normal way, but with DisabledExtensions it works.

Code: Select all

set "Hello:World=Content of hello:world"
setlocal DisableExtensions
echo %Hello:World%


And why there is a differece here

Code: Select all

setlocal enableDelayedExpansion
!=:echo Why doesn't this work?!
%=:echo Yet this does work.%
This is related to the phases, the command token is determined in the special charater phase,
so there is no problem with the percent expansion.
But for the delayed expansion it fails as != is determined as the command, as the equal sign is a command delimiter.

Code: Select all

setlocal enableDelayedExpansion
!^=:echo This works again!


hope I can help with the obvious :wink:
jeb

Re: Weird expansion - %:text% and %stripped:text%

Posted: 03 Jul 2011 03:49
by Ed Dyreen

Jeeez Jeb, do you work for Microsoft or something ? You family of Einstein ? What's your IQ ? This really isn't normal !
hope I can help with the obvious :wink:
The obvious isn't that obvious to most of us jeb :wink:

from the movie 'Waking Life':
The gap between Einstein and the average man is greater then the gap between the average man and a chimpansee.
Technologically we are at the superchimpansee level.

Re: Weird expansion - %:text% and %stripped:text%

Posted: 03 Jul 2011 13:58
by jeb
Ed Dyreen wrote:Jeeez Jeb, do you work for Microsoft or something ? You family of Einstein ? What's your IQ ? This really isn't normal !
Thanks, but my IQ is like a butter biscuit :?,
this is very helpful to understand the programmer of the cmd.exe :wink:

jeb

Re: Weird expansion - %:text% and %stripped:text%

Posted: 04 Jul 2011 09:48
by dbenham
Thanks for the reply jeb. :)

The last bit of your explanation makes the most sense for me:
jeb wrote:But for the delayed expansion it fails as != is determined as the command, as the equal sign is a command delimiter.


The earlier stuff helps to (partially) describe the behavior, but still doesn't make sense. :?

jeb wrote:A percent statement begins with a percent (wow), then it searches to the next percent or a colon.
Why does it stop at the colon :?: Or is this a side effect of some feature ? (that would be my guess). What functionality was added to CMD.EXE that prevents : from being parsed as part of name during expansion? The only special uses for : that I am aware are for labels and as a drive delimiter. Why would either of those come into play during expansion :?:

jeb wrote:If a colon is found, the variable is searched and if it isn't defined the statement ends.
Therefore you you get the rest of the line if a variable isn't defined.
It seems to be a bit more complicated than that. :wink:

Code: Select all

@echo off
setlocal
cls
set "CouldBeDefined=hello"
set "var1=(content1)"
set "var2=(content2)"
echo 1 %CouldBeDefined:var1%var2%
echo 2 %CouldBeDefined:%var1%%var2%
echo 3 %CouldBeDefined: %var1%%var2%
echo 4 %CouldBeDefined:%%var1%%var2%
echo 5 %:CouldBeDefinedvar1%var2%
echo 6 %:CouldBeDefined%var1%%var2%
echo 7 %:CouldBeDefined %var1%%var2%
echo 8 %:CouldBeDefined%%var1%%var2%
echo 9 %:var1%var2%
echo 10 %:%var1%%var2%
echo 11 %: %var1%%var2%
echo 12 %:%%var1%%var2%
Output:

Code: Select all

1 CouldBeDefined:var1(content2)
2 var1%var2
3 CouldBeDefined: (content1)(content2)
4 (content1)(content2)
5 :CouldBeDefinedvar1(content2)
6 :CouldBeDefined(content1)(content2)
7 :CouldBeDefined (content1)(content2)
8 :CouldBeDefined%var1%var2
9 :var1(content2)
10 var1%var2
11 : (content1)(content2)
12 (content1)(content2)

* If the : immediately follows the 1st %, then it is included in the display, unless it also immediately precedes a %.

* If a % immediately follows the :, then it is stripped out with the : as part of the initial expansion attempt. If there is an intervening character than the following % is available to begin a new expansion.

Again, this is all behavior, it still doesn't make sense :!: :?

Dave Benham

Re: Weird expansion - %:text% and %stripped:text%

Posted: 07 Jul 2011 07:12
by jeb
dbenham wrote:Why does it stop at the colon :?: Or is this a side effect of some feature ? (that would be my guess). What functionality was added to CMD.EXE that prevents : from being parsed as part of name during expansion? The only special uses for : that I am aware are for labels and as a drive delimiter. Why would either of those come into play during expansion :?:


:?: :?: :?:
You used it so many times that I can't believe you forget the features of the colon. :wink:

Code: Select all

set var=abcdef
set var2=%var:~1,2%
set var3=%var:abc=###%


I suppose the simple parse rule is like
1. An expansion expression begins with % or !
2. Scan each character to the first percent, colon or linefeed
2.1 If a linefeed is found use the complete current expression
2.2 if a percent is found the expression ends and a replace or substring operation can occur
2.3 if a colon is found, then the next character is important:
2.3.1 if it is a percent the colon will be a part of the variable name, nothing else
2.3.2 for each other character the variable name hasn't the colon
2.3.2.1 if the variable is defined the rest will be scannned
2.3.2.2 if the variable isn't defined the expansion expression ends with the colon, but the variable name itself nor the colon will be output

If the variable is defined but the replace or substring definition isn't legal, the complete expression will be output.

I suppose this should explain all obvious and not so obvious situations. 8)

jeb

Re: Weird expansion - %:text% and %stripped:text%

Posted: 07 Jul 2011 08:48
by dbenham
jeb wrote:You used it so many times that I can't believe you forget the features of the colon.

Code:
set var=abcdef
set var2=%var:~1,2%
set var3=%var:abc=###%

OMG You are right :!: :oops: I probably used one of those features within minutes of my post :lol: I even knew before that substring and string replacement were extension features.

Thanks jeb - it is most certainly obvious. I guess this was a case where I couldn't see the forest because of all the trees.

Dave Benham