Problem with user input in my batch file

◇◆丶佛笑我妖孽 提交于 2019-11-28 09:20:26

问题


here is the portion of code giving me trouble:

IF EXIST TH_BUILD_* (
ECHO A current build of Test Harness exists.
set /p delBuild=Delete preexisting build [y/n]?: 
if "%delBuild%"=="y" (GOTO deleteandcontinue) else ( EXIT)
)

For some reason, no matter the input, the batch file exits. Why is this happening (deleteandcontinue is never reached)?

Thanks!


回答1:


Try using delayed expansion when testing delBuild:

setlocal enableextensions enabledelayedexpansion

IF EXIST TH_BUILD_* (
    ECHO A current build of Test Harness exists.
    set /p delBuild=Delete preexisting build [y/n]?: 
    if "!delBuild!"=="y" (
        GOTO deleteandcontinue
    ) else (
        exit
    )
)

:deleteandcontinue
@echo At deleteandcontinue

%var% variables are expanded when the command is read. The set of commands between the parens are treated as a single command, so delBuild doesn't exist when you go to test. With the delayed expansion, the variables are expanded when the command is executed, so at the time of the test, delBuild has a value.



来源:https://stackoverflow.com/questions/916413/problem-with-user-input-in-my-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!