2016-09-21 7 views
1

私のスクリプトでわかるように、%pin%count %%(おそらくあなたの中には明らかに希望の値を返しませんが、望ましい変数の文字列の値を%pin5%のように返します)。バッチファイルで増加する変数の値を返す方法は?

変数の数は、ユーザーがピンに対して選択した色の数によって異なるスクリプトを作成しました。スクリプトの厄介な部分がある:

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 

私はこれを解決する方法は、%PIN1%または%PIN2%以下ではなく、値そのものを返す%%%ピン%カウントので、IF文を使用することはできませんか?

シンプルな文法上の問題のようですが、私はすべてを試していますが、まだ解決できていないし、最速の解決策かもしれません。

+2

の可能性のある重複した[Cmdを - 変数名に変数を取得](http://stackoverflow.com/questions/21278968/cmd-get-variable-in-variable-name) – aschipfl

+0

変数の技術用語は_array_で、その使用方法の説明は[この回答](http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in)に記載されています。 -cmd-exe-batch-script/10167990#10167990) – Aacini

答えて

1

複合変数名を評価するために、あなたはあなたが余分な区切り文字として!を指定することができsetlocal enabledelayedexpansionを使用する必要があり、

あなたが持っていた他の問題は、あなたが"."と変数を比較することです。バッチはbashのように引用符を削除しません。引用符を入れたり、引用符を左端にも置かないでください。

固定コード:

@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 
関連する問題