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
Note that original subroutine parameters %3 and %5 are empty!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] [] [] [] [] []

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...
