在If命令后设置命令

时间:2013-07-27 07:27:45

标签: if-statement batch-file set

尝试制作测验show.bat文件,并在使用If-Set编码时遇到问题:

:10    
echo @: (Press any key to continue)
pause >nul
cls
echo Bonus Question 1:
echo Translate the letters in CAPS.
echo @: Copy the sentence and translate the words in CAPITALS.
echo Il nome di mia ZIA e Gabriella e il nome di mio ZIO e Nick.
echo @: Careful, you only get 3 goes!
echo @: now you try!
set /p BonusAnswer1=">"
set /p BonusAnswer1=">"
if "%BonusAnswer1%"=="Il nome di mia aunty Gabriella e il nome di mio uncle Nick. echo well done! &goto:11
if "%BonusAnswer1%"=="Hint" echo @: Copy the sentence and translate ZIA and ZIO. Only put a capital for Il, Gabriella and Nick. &goto:10

这是我遇到问题的地方:

if "%Bonus1Tries%"=="3" set Bonus1Tries=2 &echo you only have %Bonus1Tries% left!

该编码中唯一不起作用的部分是:

you only have 3 tries left!

而不是

you only have 2 tries left!

这意味着

设置Bonus1Tries = 2

无效。 你能帮忙吗,并提前感谢你! -Batch Man

2 个答案:

答案 0 :(得分:3)

在代码块内需要delayed expansion,或者拆分命令(如果可能):

if "%Bonus1Tries%"=="3" set "Bonus1Tries=2"
if "%Bonus1Tries%"=="2" echo you only have %Bonus1Tries% left!

答案 1 :(得分:0)

您必须启用延迟扩展

SETLOCAL ENABLEDELAYEDEXPANSION
set VAR=before
set VAR=after & echo immediate:%VAR%, delayed:!VAR!
ENDLOCAL

Raymond Chen

解释