批处理文件PAUSE需要两次按键才能解除

Str*_*onL 1 windows scripting batch-file

我有以下代码:

@echo off
set scriptTitle=Mode Changer
title %scriptTitle%



rem Take Details
set /p machine=Enter machine name:%=%
echo. %machine%
set /p rMSNewMode=Mode (t/l):%=%
echo. %rMSNewMode%

rem Perform Task
cls

rem Check machine existence
ping %machine% -n 1
if %errorLevel% == 1 call:error "The specified machine (%machine%) could not be located."


:error
color 4f
title ERROR: %scriptTitle%
cls
echo. The following error has occurred:
echo.
echo. %~1
echo.
pause
goto EOF
Run Code Online (Sandbox Code Playgroud)

批处理文件完成后,我按预期"按任意键继续".但是,它需要在批处理文件结束之前按两次"任意键".如果我替换以下行似乎:

if %errorLevel% == 1 call:error "The specified machine (%machine%) could not be located."
Run Code Online (Sandbox Code Playgroud)

if %errorLevel% == 1 goto error
Run Code Online (Sandbox Code Playgroud)

我只被提示一次.

有什么问题?

Mag*_*goo 5

与许多语言不同,批处理没有"程序"结束的概念 - 它只是逐行继续执行,直到它到达文件结尾.因此,您需要goto :eof在完成主线之后,否则执行将继续通过子例程代码.:EOF是一个预定义的标签CMD,意思是end of file.结肠是必需的.

当你call :label在结束文件批量退货(因为标签call后ED)的声明call-这是color 4f在内部error程序.pause刚刚在返回之前执行了批处理,批量只是逐行收费,直到它再次遇到pause- 因此两个提示/响应周期.