在“退出”命令后不退出

时间:2019-03-22 17:40:01

标签: batch-file

我正在编程,遇到问题。

if exist savefile.climax (
echo It appears you have one...
echo Checking your data...
(
set /p name=
set /p level=
)<savefile.climax
) else (
echo Oh man, you don't have one.
echo Would you like to create it?
choice /c yn /n /m "[Y]es or [N]o?"
if %errorlevel% == 1 goto creation
if %errorlevel% == 2 exit
)

并且您可以看到,如果错误级别为2,则程序应该退出,但不是。它继续进行创建代码。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

之所以从未到达出口,是因为您的%errorlevel%获得了set并在代码块内使用,因此您需要enabledelayedexpansion。也就是说,如果没有它,您可以逃脱:

@echo off
if exist savefile.climax (
    echo It appears you have one...
    echo Checking your data...
    (
    set /p name=
    set /p level=
    )<savefile.climax
) else (
    echo Oh man, you don't have one.
    echo Would you like to create it?
    choice /c yn /n /m "[Y]es or [N]o?"
    if errorlevel 2 exit
    if errorlevel 1 goto creation
)

答案 1 :(得分:-1)

我自己找到了解决方案。我把

if %errorlevel% == 1 goto creation
if %errorlevel% == 2 exit
像这样

超出代码块。

if exist savefile.climax (
echo It appears you have one...
echo Checking your data...
(
set /p name=
set /p level=
)<savefile.climax
) else (
echo Oh man, you don't have one.
echo Would you like to create it?
choice /c yn /n /m "[Y]es or [N]o?"
)

if %errorlevel% == 1 goto creation
if %errorlevel% == 2 exit