Computing.Net > Forums > Programming > Nesting variables in batch files ?

Nesting variables in batch files ?

Reply to Message Icon

Original Message
Name: andrewjo
Date: May 16, 2006 at 15:06:02 Pacific
Subject: Nesting variables in batch files ?
OS: Windows XP sp2
CPU/Ram: Centrino
Model/Manufacturer: Dell
Comment:

I have a batch file which displays a list of databases and allows the user to choose the one that they wish to start by typing in the name of the database (I use a "set /p" command to get the user input). This worked fine when there were only 3 or 4 databases in the list. Now, there are quite a few databases in the list, and some of the database names are a bit lengthy. So, I tried to simplify the user input by numbering all the databases in the displayed list and asking the user to just enter the number of the database that they wish to start, instead of its name. However, I got stuck on how to match the number that the user enters to the correct item in the list ... I haven't worked out how to combine my variables together correctly - hopefully it is possible in batch! I cannot use any third party utilities, and wish to avoid writing temporary files to hold variable values.

Here are the relevant lines from my batch file. The problem is in the last line ... the answer is probably staring right at me, but my excuse is I am a newcomer to batch files :)

Any help much appreciated:


:list_db_names
set db_count=1
for /f "tokens=3 delims=) " %%a in ('find /i "global" listener.ora') do call :db_choice %%a
goto get_db_name

:db_choice
set db_name%db_count%=%1
echo %db_count%. %1
set /a db_count+=1
goto :eof

:get_db_name
set /p db_num="Enter the number of the database you want to start: "

set database_name=%db_name%%db_num%

...


Thanks


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: May 17, 2006 at 00:53:53 Pacific
Reply: (edit)

I don't know why you said you have problems combining the variables.

H:\>set two=2
H:\>set seven=7
H:\>set combine=%two%%seven%
H:\>echo %combine%
27

To compare numeric values:

if %userinput% EQU 1 .....
if %userinput% EQU 2 ....


Report Offensive Follow Up For Removal

Response Number 2
Name: andrewjo
Date: May 17, 2006 at 03:35:29 Pacific
Reply: (edit)

Thanks for the reply ghostdog. I think my problem arises bacause one of my variables has a name partly composed of another variable:

set db_name%db_count%=%1

This line will create lots of variables named "db_name1", "db_name2", "db_name3", ie, one for each database. The problem i have is referring back to the correct variable after I get my user input. So if the user inputs "5", then I want to be able to refer to the value held in variable "db_name5". That's why I try to append the user input to "db_name" to create the name of the variable i want to refer to in this line:

set database_name=%db_name%%db_num%

I have a feeling I need to enable delayed expansion and then do somthing like this:

set database_name=!db_name%db_num%!

In fact, I have just tried that and IT WORKS! (I spent hours trying to work this out yesterday!)

Sorry if I wasted your time, I shoulda thought some more about the problem before i posted.

Many thanks for your assistance.


Report Offensive Follow Up For Removal

Response Number 3
Name: ghostdog
Date: May 17, 2006 at 06:07:05 Pacific
Reply: (edit)

oh...i see..
well ... the !db_name%db_num%! thing is also described in set help page , set /?
.......
.......
Delayed environment variable expansion allows you to use a different character (the exclamation mark) to expand environment variables at execution time. If delayed variable expansion is enabled, the above examples could be written as follows to work as intended:
set VAR=before
if "%VAR%" == "before" (
set VAR=after
if "!VAR!" == "after" @echo If you see this, it worked
)

set LIST=
for %i in (*) do set LIST=!LIST! %i
echo %LIST%
....
....



Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Nesting variables in batch files ?

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge