4

我正在编写一个简单的 .bat 脚本,我希望它通过从 Windows 资源管理器中双击来打开它。如果从 Windows 资源管理器打开,控制台窗口会在脚本完成后自动关闭。

问题是我在脚本中调用的命令可能会发生错误。在这种情况下,这些命令会打印出错误消息并返回退出状态 1。但用户不知道它,因为窗口关闭得太快而无法注意到是否弹出了一些错误消息(并且肯定太快而无法阅读它们;))。

那么,如果脚本中的某些命令失败(以便用户可以阅读错误消息)并且如果一切正常,它仍然会自动关闭,是否有任何方法可以防止控制台窗口关闭?如果第二部分是不可能的,可以只阻止控制台窗口在两种情况下自动关闭(失败或成功)。

4

1 回答 1

5

It would be much easier to go with the latter. To keep the console window open put this at the end of your script:

pause >nul

That will pause the window from closing. Then you can handle any errors you want to display and presumably echo them to the user before the pause.

Example:

echo No errors or Errors found!

pause >nul

Hope this helps!

于 2012-04-10T09:01:41.973 回答