Even if you read once that it involves nestling a call within a call (within a call... and so on), an explanation and an example might not have been provided. So I'll do that.
First the explanation:
Everybody knows, for one level using enabledelayedexpansion, you write:
Code: Select all
set number=1
set log1=Hello
echo:!log%number%!
Forget this for now, I'll get back to it further on.
Let's say we have set var=hello
Every %% expands to %. Then every %var% expands to hello
So,
%var% expands to hello
%%var%% expands to %var%
%%%var%%% expands to %hello%
%%%%var%%%% expands to %%var%%
%%%%%var%%%%% expands to %%hello%%
%%%%%%var%%%%%% expands to %%%var%%%
...and so on.
Each time you use call, it will expand on top of Command Prompt's expansion.
So,
call %var% expands to hello expands to hello (does nothing)
call %%var%% expands to %var% expands to hello
call %%%var%%% expands to %hello% expands to nothing (variable hello doesn't exist)
call %%%%var%%%% expands to %%var%% expands to %var%
call %%%%%var%%%%% expands to %%hello%% expands to %hello%
call %%%%%%var%%%%%% expands to %%%var%%% expands to %hello%
...and...
call %%%%%%%var%%%%%%% expands to %%%hello%%% expands to %% (variable hello doesn't exist)
...and so on.
"Ok, get on to the call nestling. How do you figure out how many % signs to write?"
It's easy, the formula is:
Where n is the nested level,
2^n = Number of % signs on each side.
So starting from the Command Prompt level, take a look at this:
Code: Select all
@echo off
:: Level 0 (2^0=1. One % sign.)
set a_cup=pot
echo:%a_cup%
:: Level 1 (2^1=2. Two % signs.)
set a_pot=box
call echo:%%a_%a_cup%%%
:: Level 2 (2^2=4. Four % signs.)
set a_box=crate
call call echo:%%%%a_%%a_%a_cup%%%%%%%
:: Level 3 (2^3=8. Eight % signs.)
set a_crate=boat
call call call echo:%%%%%%%%a_%%%%a_%%a_%a_cup%%%%%%%%%%%%%%%
:: Level 4 (2^4=16. Sixteen % signs.)
set a_boat=Hello World
call call call call echo:%%%%%%%%%%%%%%%%a_%%%%%%%%a_%%%%a_%%a_%a_cup%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
So, about enabledelayedexpansion, you can do an extra level of nestling to remove one of the calls. If you're using enabledelayedexpansion, this is probably faster than using an extra call, so I would recommend it. You might wonder how this effects the 2^n formula. Well, you're just pushing the calls forward by one expansion, so subtract 1 from each level (except Level 0 stays 0). Level 1 becomes 0, Level 2 becomes 1, etc... 2^(n-1)
Code: Select all
@echo off&setlocal enabledelayedexpansion
set a_cup=pot
echo:%a_cup%
set a_pot=box
echo:!a_%a_cup%!
set a_box=crate
call echo:%%a_!a_%a_cup%!%%
set a_crate=boat
call call echo:%%%%a_%%a_!a_%a_cup%!%%%%%%
set a_boat=Hello World
call call call echo:%%%%%%%%a_%%%%a_%%a_!a_%a_cup%!%%%%%%%%%%%%%%