Windows - prevent the console window from closing when errors occur

I am writing a simple .bat script and I want it to open it with a double click from Windows Explorer. If it is opened from Windows Explorer, the console window will automatically close after the script finishes.

The problem is that errors can come from the commands I call in the script. In this case, these commands print error messages and return exit status 1. But the user is unaware of this because the window closes too quickly to notice that some error messages have exited (and certainly too quickly to read them;)) ...

So, is there a way to prevent the console window from closing if some command in the script fails (so the user can read the error message) and still automatically closes if everything works fine? If the second part is not feasible, just avoid automatically closing the console window in both cases (either failure or success).

+3


source to share


1 answer


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

pause >nul

This will stop the window from closing. You can then handle whatever errors you want to display, and presumably echo

to the user, before pause

.

Example:



echo No errors or Errors found!

pause >nul

Hope this helps!

+5


source







All Articles