4

I'm writing a simple .bat script and I want it to open it by double click from Windows Explorer. If opened from Windows Explorer, console window automatically closes after script is finished.

The problem is that errors may occur from commands that I invoke in a script. In this case these comands print out error messages and return exit status 1. But user don't know about it because Window closes too fast to notice if some error messages popped out (and definitely too fast to read them ;) ).

So is there any way to prevent console window from closing if some command in script failed (so that user can read error message) and still having it automatically closed if everything worked fine? If the second part is not possible, it's okay to just prevent console window to close automatically in both cases (either failure or success).

Piotr Sobczyk
  • 6,443
  • 7
  • 47
  • 70

1 Answers1

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!

Bali C
  • 30,582
  • 35
  • 123
  • 152