Solved Use a Variable as part of another Variable

August 6, 2009 at 04:35:30
Specs: Windows XP Professional SP3, 512 MB
Is it possible to use a Variable in another

For instance, in my batch file I have
SET /p UCode=

* Example:
* SET /p UCode=Cas123

Cas123 will be a random number/code,
specific to the user.

The User Code will be used in multiple places,
right through the batch file.

The Type will be specified.

* Example:
* SET Type=Daily
(There will be Weekly, Monthly, Yearly types
as well. But in different batch files.)

Later the batch file uses the %date% %time%
Variable as well as the %Type% to create a
new Variable, based on the User Code.

* Example:
* SET %UCode%_Type=%date% %time%

This creates a Variable:
Cas123=2009/08/06 9:38:16.00 Daily

My problem is this:
I need to ECHO %UCode%_Type to the
(It will also be directed to a log file.)

How do I ECHO it?

ECHO %%UCode%_Type% displays as

ECHO %UCode%_Type% displays as

How do I get it to display as
Cas123=2009/08/06 9:38:16.00 Daily?

See More: Use a Variable as part of another Variable

Report •

August 6, 2009 at 06:01:03
✔ Best Answer
The easiest way to do it is this:

SET %UCode%_Type

The SET command, used without an = sign, will echo the value of the given variable. To be more precise, it echoes the values of all variables beginning with the given prefix. But in your case I guess it will only match one variable.

There is another technique, which is a little harder to use but can then be used in more general use cases. Using a feature known as delayed expansion, you can use the !var! syntax together with the %var% syntax:

setlocal enabledelayedexpansion
ECHO !%UCode%_Type!

Report •

August 7, 2009 at 02:29:49
Thank you!
That worked great.

Report •

August 7, 2009 at 02:45:58
This is probably going to sound dumb.

Do I have to close (mark as answered) this topic, or will a
moderator close it?
If I have to, how do I go about closing it?

Sorry for bothering.

Report •

Related Solutions

August 7, 2009 at 03:34:07
No need to do anything. As far as I know, all topics on this forum are open-ended (except those that are months old which get closed automatically.)

This allows anyone to add a reply at any time. For example, although you said this question was answered, someone else may reply in a few days' time with a better way of solving the problem, which would add value to the discussion for all Internet users to see.

Report •

August 7, 2009 at 05:28:11
I wouldn't call these better ways but other ways:

Report •

Ask Question