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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Nesting variables in batch files ?

Reply to Message Icon

Name: andrewjo
Date: May 16, 2006 at 15:06:02 Pacific
OS: Windows XP sp2
CPU/Ram: Centrino
Product: 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



Sponsored Link
Ads by Google

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

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 ....


0

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

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.


0

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

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%
....
....



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


What the heck are binary ... Batch File for Filname Co...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Nesting variables in batch files ?

Variables in Batch Files www.computing.net/answers/programming/variables-in-batch-files/14347.html

Nested loop in batch file help www.computing.net/answers/programming/nested-loop-in-batch-file-help/19396.html

Date Routines in Batch Files www.computing.net/answers/programming/date-routines-in-batch-files/15590.html