Help using multiple variables

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Help using multiple variables

#1 Post by chr15b » 17 Jan 2012 08:04

Hi, wonder if you could help me with something that should be quite simple.

I'm trying to use the contents of variables within other variables but its not doing what i want

as an example:

Set Client1=fred
Set Client2=joe

SET CLIx=0

SET /A CLIx=%CLIx%+1
---------------------------

What command would i need to use to echo Fred then Joe?

Echo Client%CLIx% outputs "Client1"

Thanks in advance :)
Chris

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

Re: Help using multiple variables

#2 Post by dbenham » 17 Jan 2012 09:00

Relatively slow way:

Code: Select all

call echo %%Client%CLIx%%%

Fast way under normal circumstances using delayed expansion

Code: Select all

setlocal enableDelayedExpansion
echo !Client%CLIx%!

Fast way if %CLIx% does not have desired value (if it was set within a parenthesized code block and you are still in the same code block)

Code: Select all

for /f %%N in ("!CLIx!") do echo !Client%%N!


For more info about delayed expansion, type HELP SET from the command line and read the section that starts with "Finally, support for delayed environment variable expansion..."

Dave Benham

chr15b
Posts: 23
Joined: 17 Jan 2012 07:59

Re: Help using multiple variables

#3 Post by chr15b » 17 Jan 2012 09:02

Fantastic, will have a play... thanks very much :)

Post Reply