问题在我的批处理文件,用户输入 [英] Problem with user input in my batch file
问题描述
这里code的部分给我找麻烦:
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)
)
由于某些原因,无论输入,该批处理文件退出。这究竟是为什么(永远达不到deleteandcontinue)?
For some reason, no matter the input, the batch file exits. Why is this happening (deleteandcontinue is never reached)?
谢谢!
推荐答案
尝试测试在使用延迟扩展 delBuild
:
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%
当读取命令变量扩展。该组括号之间的命令被视为一个单一的命令,这样 delBuild
当你去测试不存在。随着延迟的扩张,这些变量在执行命令时,在测试时扩大,因此, delBuild
有一个值。
%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.
这篇关于问题在我的批处理文件,用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!