Page 1 of 1

Variable Within a Variable

Posted: 22 May 2011 16:19
by alleypuppy
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!

Re: Variable Within a Variable

Posted: 22 May 2011 17:52
by dbenham
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

Re: Variable Within a Variable

Posted: 22 May 2011 21:11
by Cleptography
You could also use delayedexpansion to do the same !%n%!

Re: Variable Within a Variable

Posted: 22 May 2011 21:37
by alleypuppy
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.

Re: Variable Within a Variable

Posted: 22 May 2011 22:55
by Cleptography
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.

Re: Variable Within a Variable

Posted: 23 May 2011 02:23
by orange_batch
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%!