How to prevent batch window from closing when error occurs?

Batch File

Batch File Problem Overview


I'm trying to write batch script to create a folder if it does not already exist. Following up the online examples, below is my script.

The problem is; first pause works, then probably due to syntax error the window closes even before reaches to the second pause, so I can't really tell which part of my script is wrong.

Could anyone show me how to prevent closing window so that I can see what's on the window?

@echo off

:copy theme images over
:designer
echo copying theme images over...
pause
if not exist "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text"
(
	md "%K2DIR%\K2 SmartForms Runtime\Styles\Themes\Sharepoint 2013\rich_text333"
)

pause

Batch File Solutions


Solution 1 - Batch File

You could put this line at the beginning of the batch file:

if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit )

What this line does is, the first time you run it, it re-launches itself in a subprocess that doesn't exit after it finishes running the batch file.

Solution 2 - Batch File

You need to pass the /K switch to CMD, or just open a Command Window and run the batch from the command line.

Solution 3 - Batch File

Press start and type cmd and press enter, you will launch a command prompt.

Just drag and drop what you need to run (your python script, .exe ...) into the cmd windows, and press enter.

(You might some time to run the cmd as admin: find the cmd in the start menu, right-click on it, choose run as admin).

Solution 4 - Batch File

I recorded the screen (bandicam) for when I couldn't quite read the error message, and then I could replay it; I suppose this is mainly helpful if you already have software on your computer.

Solution 5 - Batch File

> How to prevent batch window from closing when error occurs?

I had the problem when using robocopy. My solution was:

if not %errorlevel% lss 8 pause

For Robocopy every exit code below 8 is a success: https://ss64.com/nt/robocopy-exit.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMeowView Question on Stackoverflow
Solution 1 - Batch FileKlitos KyriacouView Answer on Stackoverflow
Solution 2 - Batch FileAMissicoView Answer on Stackoverflow
Solution 3 - Batch FileJinSnowView Answer on Stackoverflow
Solution 4 - Batch FileDarth TaterView Answer on Stackoverflow
Solution 5 - Batch Fileuser2029101View Answer on Stackoverflow