Batch- Displaying a Variable in a Variable

September 5, 2010 at 16:28:46
Specs: Windows XP
Here's an example of what my code looks like:
set mes1=This is message 1.
set mes2=This is message 2.
set mes3=This is message 3.
set mes4=This is message 4.
set mes5=This is message 5.
set mes6=This is message 6.
set mes7=This is message 7.
set mes8=This is message 8.
set mes9=This is message 9.
set mes10=This is message 10.
set mes11=This is message 11.
set /a ran=%random%%%11+1
set message=mes%ran%
ECHO Message: %message%

I want it to generate a random number from 1 to 11, and display the contents of the variable
%message% is equal to, like if %message% equals 1 it should display "Message: This is Message 1", but instead of displaying the contents of the variable it's equal to, it displays the name of it. (E.g. "Message: mes1")
Help is greatly appreciated.

See More: Batch- Displaying a Variable in a Variable

Report •

September 5, 2010 at 21:10:33
There are two ways to do this:

1. Use call.

call ECHO Message: %%%message%%%

2. Use delayed expansion.

SetLocal EnableDelayedExpansion

rem the rest of your script.....

ECHO Message: !%message%!

A little more info:

Report •

September 6, 2010 at 08:08:49
Thanks, worked right well fine.

Report •

Related Solutions

Ask Question