Meow
Meow

Reputation: 19071

How to prevent batch window from closing when error occurs?

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

Upvotes: 68

Views: 82689

Answers (6)

beeto45
beeto45

Reputation: 11

using pause at end of batch paused the cmd screen, tanks!

Upvotes: 1

user2029101
user2029101

Reputation: 196

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

Upvotes: 2

JinSnow
JinSnow

Reputation: 1663

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).

Upvotes: 9

Klitos Kyriacou
Klitos Kyriacou

Reputation: 11621

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.

Upvotes: 86

Darth Tater
Darth Tater

Reputation: 477

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.

Upvotes: 1

AMissico
AMissico

Reputation: 21684

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

Upvotes: 44

Related Questions