CMD batch variable cascade issue

April 14, 2010 at 05:38:34
Specs: Windows XP
I hope someone can help me with this, as I've been looking all over and cannot find a solution to what I'm sure is an easy problem!

I have a batch file that defines a number of target variables, e.g.:
set target1=Sales
set target2=Marketing
set target3=Finance

There is also a numeric variable ("num"), that is used to make the "activetarget" variable:
set activetarget=target%num%

This results in a variable that looks like:

However, what I actually need to achieve is:

In this example, how can I set the VALUE of "activetarget" to be the current VALUE (not the name!) of the "target1" variable??

Thanks in advance!

See More: CMD batch variable cascade issue

Report •

April 14, 2010 at 06:33:40
Start with:

@echo off & setLocal EnableDELAYedeXpansion & set L=

and change:

set activetarget=target%num%


set activetarget=!target%num%!

Helping others achieve escape felicity


Report •

April 14, 2010 at 06:42:44
You have to enable the "Delayed Expansion" that allows the use of "variable inside variable".

@echo off & setlocal EnableDelayedExpansion
set activetarget=!target%num%!

where the outer variable is marked by ! and the inner one by the usual %. The delayed expansion is needed to enable other advanced behaviors of environment variables.

To know more on the issue type set /?.

Report •

April 15, 2010 at 07:11:15
M2 & IVO - thank you both very much for your replies. That did the trick and has solved a major headache.

Always great to learn new things. Thank you for your help!

Report •
Related Solutions

Ask Question