windows - 批处理文件暂停需要两次按键才能关闭

标签 windows batch-file scripting

我有以下代码:

@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

批处理文件完成后,我会按预期收到“按任意键继续”。但是,它需要在批处理文件结束之前按两次“任意键”。如果我替换以下行,它就会出现:

if %errorLevel% == 1 call:error "The specified machine (%machine%) could not be located."

if %errorLevel% == 1 goto error

我只收到一次提示。

问题是什么?

最佳答案

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

当你call :label批处理在文件末尾(因为标签是 call ed)返回到 call 之后的语句- 这是 color 4ferror 内常规。刚刚执行了 pause就在返回之前,batch 会逐行充电,直到再次遇到 pause。 - 因此有两个提示/响应周期。

关于windows - 批处理文件暂停需要两次按键才能关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24317746/

相关文章:

c++ - 计算单色 BMP 的 BITMAPINFOHEADER biCompression 值

windows - 如何在Windows批处理模式下获取没有文件扩展名的完整路径的文件名

api - 如何使用脚本从 LexisNexis 获取文章

javascript - 缺少 "PLAIN"nodemailer 的凭据

Windows上的C自动启动程序无法使用文件

windows - IIS 日志文件和负载平衡器?

c++ - 有没有办法确定日期/时间是否不存在?

batch-file - 重命名文件以在文件名中包含修改日期

regex - 正则表达式以匹配批处理脚本中的变量

bash-回显 : write error: invalid argument