double expansion of variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dizze
Posts: 10
Joined: 12 Jul 2006 07:53
Contact:

double expansion of variables

#1 Post by dizze » 12 Jul 2006 07:54

Hello,

Is it possible to echo the value of a variable when the name of the variable is itself a variable.

I have (stripping other nonsence)

:loop
set cols=0
set /a c%cols%=%random%
set /a cols=%cols%+1
if cols==10 (goto :wherever) else (goto :loop)


And i want to be able to echo whatever %random% is which will be stored under whatever value is generate by c%cols%

This required (i think) a double expansion of a variable, but i cannot convince dos to do this.

Any ideas?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 15 Jul 2006 20:47

dizze,

Yes you can, in your case use:

Code: Select all

call echo.%%c%cols%%%

Before executing the line the command interpreter will resolve:

%cols% --> value of cols variable
%% ------> %


Assuming the value of the cols variable is 0 and the value of c0 is 12345 this means:

Code: Select all

call echo.%%c%cols%%%

will be resolved to:

Code: Select all

call echo.%c0%

Then the command line will be executed starting with call command which again will make the command interpreter resolve all variables first:
echo.12345

Hope this helps :wink:

dizze
Posts: 10
Joined: 12 Jul 2006 07:53
Contact:

Sucess!

#3 Post by dizze » 17 Jul 2006 09:39

Thanks DosItHelp :D ,

That works perfectly, i can now get rid of my tempory file hack.

Dizz-E

Post Reply