如何在批处理文件中返回增加变量的值?

时间:2016-09-21 19:05:29

标签: batch-file

正如您在我的脚本中所看到的那样,%pin%count %%(显然对于你们中的一些人)不会返回所需的值,而是返回所需变量的字符串值,如%pin5%for实例

我创建了一个脚本,其中变量的数量取决于用户为其引脚选择的颜色数量。脚本令人不安的部分是:

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

我不能使用IF语句因为%pin%count %%返回%pin1%或%pin2%而不是值本身,如何解决这个问题?

这似乎是一个简单的语法问题,但我尝试了所有的东西并且还没有设法解决它并且要求可能是最快的解决方案。

1 个答案:

答案 0 :(得分: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
相关问题