1

As you can see in my script bellow, the %pin%count%% (maybe obviously for some of you) won't return the wanted value but the string value of the wanted variable, as %pin5% for instance.

I've created a script where the number of variables will depend on how many colors the user chose for his pins. The troubling part of the script is:

Echo - Please type the colors of the pins allowed in the purchase, 
or type dot (.) to finish this part of the script.
set count=0
:Pin
set /a count=%count%+1
set /p pin%count%=
if not %pin%count%%=="." goto Pin

I cannot use the IF statement because %pin%count%% returns %pin1% or %pin2% but not the value itself, how to solve this?

It seems like a simple enough syntax problem, but i'm trying everything and haven't managed to solve it yet and asking may be the fastest solution.

Mitrek
  • 1,222
  • 8
  • 10
  • 2
    Possible duplicate of [Cmd - get variable in variable name](http://stackoverflow.com/questions/21278968/cmd-get-variable-in-variable-name) – aschipfl Sep 21 '16 at 19:45
  • The technicall term for your variables is _array_ and a description of its use is given in [this answer](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990) – Aacini Sep 21 '16 at 19:54

1 Answers1

1

to evaluate a composite variable name, you have to use setlocal enabledelayedexpansion so you can specify ! as an extra delimiter,

The other problem you had is that you compared the variable with ".". Batch does not remove quotes like bash does. Don't put the quotes, or put some quotes on the left end too.

Fixed code:

@echo off
Echo - Please type the colors of the pins allowed in the purchase, 
echo or type dot (.) to finish this part of the script.
setlocal enabledelayedexpansion
set count=0
:Pin
set /a count+=1
set /p pin%count%=
rem echo the variable for debug purposes
echo pin%count% = !pin%count%!
rem here's the tricky line
if not !pin%count%!==. goto Pin
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219