Very strange behavior in empty subroutine parameters!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Very strange behavior in empty subroutine parameters!

#1 Post by Aacini » 29 Mar 2012 23:01

I was done some tests trying to assemble the list of parameters to a subroutine in a variable, that consists in several variable values (with Delayed Expansion disabled), and later Enabling Delayed Expansion and calling the subroutine via CALL :THESUB !PARAMETERS!. However, I found something very strange: if a not defined variable is expanded via !delayed! expansion, it leave a "hole" in the list of parameters, that is, it create an "empty parameter" that occupy a place in the parameter list that is even displaced by SHIFT command.

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set var1=VarOne
set var2=VarTwo
set "parameters=%%var1%% %%noDef1%% !var2! !noDef2! Param !noDef3! Last"
setlocal EnableDelayedExpansion
echo call :theSub !parameters!
call :theSub !parameters!
goto :EOF


:theSub
echo/
echo Entering theSub
echo All the parameters: [%*]
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
exit /B
Output wrote:call :theSub %var1% %noDef1% !var2! !noDef2! Param !noDef3! Last

Entering theSub
All the parameters: [VarOne VarTwo Param Last]
Individual params: [VarOne] [VarTwo] [] [Param] [] [Last]
Individual params: [VarTwo] [] [Param] [] [Last] []
Individual params: [] [Param] [] [Last] [] []
Individual params: [Param] [] [Last] [] [] []
Individual params: [] [Last] [] [] [] []
Individual params: [Last] [] [] [] [] []
Note that original subroutine parameters %3 and %5 are empty! :shock:

There are a couple strange things about this program:

- When CALL :THESUB !PARAMETERS! is first delayed expanded, it result in:

CALL :THESUB %VAR1% %NODEF1% !VAR2! !NODEF2! PARAM !NODEF3! LAST

At this step the CALL command cause a restart of parse phase that replace %VAR1% and %NODEF1% by its values, but supposedly the Delayed Expansion is NOT restarted, so !VAR2!, !NODEF2! and !NODEF3! should NOT be expanded. Why these variables are replaced by their values?

- Independently of the expansion order that may cause the previous replacement, when an empty variable is expanded it leaves "nothing". If both %normal% and !delayed! expansions are achieved before executing the command, then both empty variables must be replaced by "nothing" and hence not appear in the subroutine parameters. How is possible that previous delayed expansion create an "empty parameter". This bug/feature/whichever prevent to use a loop combined with SHIFT command to process ALL the parameters!

I am really confused... :? Anybody have an explanation?

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Very strange behavior in empty subroutine parameters!

#2 Post by Ed Dyreen » 30 Mar 2012 00:13

'

Code: Select all

@echo off
setlocal DisableDelayedExpansion
set var1=VarOne
set var2=VarTwo
set "parameters=%%var1%% %%noDef1%% !var2! !noDef2! Param !noDef3! Last"
setlocal EnableDelayedExpansion
echo call :theSub !parameters!
call :theSub !parameters! %= parameters is expanded only once at execution =%
pause
goto :EOF

:theSub
echo/
echo Entering theSub
setlocal DisableDelayedExpansion %= <<<<<<<<<<<<<<< =%
echo All the parameters: [%*]
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
echo Individual  params: [%1] [%2] [%3] [%4] [%5] [%6]
shift
endlocal %= <<<<<<<<<<<<<<< =%
exit /B

Code: Select all

call :theSub %var1% %noDef1% !var2! !noDef2! Param !noDef3! Last

Entering theSub
All the parameters: [VarOne  !var2! !noDef2! Param !noDef3! Last]
Individual  params: [VarOne] [!var2!] [!noDef2!] [Param] [!noDef3!] [Last]
Individual  params: [!var2!] [!noDef2!] [Param] [!noDef3!] [Last] []
Individual  params: [!noDef2!] [Param] [!noDef3!] [Last] [] []
Individual  params: [Param] [!noDef3!] [Last] [] [] []
Individual  params: [!noDef3!] [Last] [] [] [] []
Individual  params: [Last] [] [] [] [] []
Druk op een toets om door te gaan. . .
What output did you expect ?

Aacini
Expert
Posts: 1932
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Very strange behavior in empty subroutine parameters!

#3 Post by Aacini » 30 Mar 2012 01:54

I see it now! :D The second delayed expansion happen inside the subroutine! I was confused because the code I was testing before reach this point. :?

Thanks a lot, Ed! 8)

Antonio

Post Reply