简体   繁体   English

批处理脚本 - 如何在出错时暂停(按任意键继续......)?

[英]Batch Script - How to pause (Press any key to continue . . .) on error?

I would like my script to execute the pause command whether the script completes, or if an error/exception if thrown.我希望我的脚本执行pause命令,无论脚本是否完成,或者如果抛出错误/异常。

Basically, I want the script to behave as if I placed基本上,我希望脚本的行为就像我放置

setlocal enabledelayedexpansion
REM Keep cmd window open until script finishes in case of error
if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )

at the top of the script, but give the Press any key to continue.在脚本的顶部,但按任意键继续。 . . . . option to exit/close the cmd window.退出/关闭 cmd window 的选项。

Thanks: Here is what I ended up with for general use:谢谢:这是我最终得到的一般用途:

@echo off

REM ##########################################################################################################
REM  Functions
REM ##########################################################################################################
goto :main

REM ##########################################################################################################
REM  run_and_keep_window_open_with_press_key_to_close_window
REM ##########################################################################################################
:run_and_keep_window_open_with_press_key_to_close_window

    echo "hi there"
    sfgsdgfdfg

goto :eof

REM ##########################################################################################################
REM  Main
REM ##########################################################################################################
:main

call :run_and_keep_window_open_with_press_key_to_close_window
if %errorlevel% EQU 0 (pause) else (color CF && pause)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM