Variable Within a Variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Variable Within a Variable

#1 Post by alleypuppy » 22 May 2011 16:19

Hi!

I'm having some trouble with variables. I am calling one batch file from another, and in the batch file that I call, I set variable n to a number and user%n% to "user[number]." What I need to do is echo user%n% in the main batch file but I can't figure out how to do it!

I'm trying to do:

set command=^>
set n=%n%
if /I "%command%"=="%n%" echo %user%n%%

but it won't work. I know why, but I can't think of another way to echo user%n%

Thanks for any help!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Variable Within a Variable

#2 Post by dbenham » 22 May 2011 17:52

I'm having a hard time figuring out exactly what you want the output to look like. But it sounds like you need to escape the %. Try:

Code: Select all

echo user%%n%%


Dave Benham

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Variable Within a Variable

#3 Post by Cleptography » 22 May 2011 21:11

You could also use delayedexpansion to do the same !%n%!

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Variable Within a Variable

#4 Post by alleypuppy » 22 May 2011 21:37

Cleptography wrote:You could also use delayedexpansion to do the same !%n%!

Do you mean you want me to do:

if /I "%command%"=="%n%" echo !user%n%!

dbenham wrote:I'm having a hard time figuring out exactly what you want the output to look like. But it sounds like you need to escape the %. Try:

Code: Select all

echo user%%n%%


Dave Benham

dbenham, I'm not trying to escape %. Normally, I would have to put:

Code: Select all

if /I "%command%"=="1" echo %user1%
if /I "%command%"=="2" echo %user2%

etc.

to achieve what I'm trying to do, but instead of making a huge list like this, I want to pull the n variable which was specified in a previously called batch file, and use it to replace the number. For instance, instead of echoing user1, I want to echo the user%n%, with %n% being the number that I typed.

Sorry if this is still sort of confusing, it's hard to explain. If you'd like, I could post the entire file so you guys could mess with it.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Variable Within a Variable

#5 Post by Cleptography » 22 May 2011 22:55

alleypuppy wrote:
Cleptography wrote:You could also use delayedexpansion to do the same !%n%!

Do you mean you want me to do:

if /I "%command%"=="%n%" echo !user%n%!


Kind of... That is one way of passing variables from within variables.
If you passed them via the command line something like.

script.bat arg1

Code: Select all

setlocal enabledelayedexpansion
set n=1
echo.!n%1!


Except in your above case user expands to a value as does %n%

I apologize for my bad explanations my tech talk is very poor. I just know
how to do these things and not always how to explain them. I used this for
a script that called a text file that had
menuItem1=SomeScript1.bat
menuItem2=SomeScript2.bat

Then in my main script after the text file had been loaded I used

Code: Select all

set /p opt=?-
REM Now check the user commands and pass them to the related scripts with args
REM (%%a = MENU config file) (%%b = command) (%%c = arguments)
for /f "tokens=*" %%a in (%path2menus%) do (
   for /f "tokens=1*" %%b in ("%opt%") do (
      if /i "%%c"=="" (set opt=%%b /?&& goto :CHECK-COMMAND-OPTIONS
      ) else if /i "%%a"=="/*" (REM USED FOR COMMENTS AND LABELS
      ) else if /i "%%a"=="%%b" (call !path2%%b! %%c&& goto :[opt] <---This is the line you are interested in
)))


The script above would call some other script taken as the path var and %%c as %1 %2 %3 etc...
This was done so that anytime a new menu item needed to be created I could simply just add it to
the text file which had
Script1
Script2
Script3
Instead of having to add more code to my script.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Variable Within a Variable

#6 Post by orange_batch » 23 May 2011 02:23

viewtopic.php?t=1486

Everybody knows, for one level using enabledelayedexpansion, you write:

Code: Select all

set number=1
set log1=Hello
echo:!log%number%!

Post Reply