You are on page 1of 2

12/2/2021 How to prevent batch window from closing when error occurs?

- Stack Overflow
 

How to prevent batch window from closing when error occurs?


Asked 8 years, 6 months ago Active 9 days ago Viewed 47k times

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.

49 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?

8
@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

Share Improve this question Follow edited Jun 15 '13 at 0:06 asked Jun 14 '13 at 23:50
pattyd Meow
5,499 10 36 56 16.7k 50 125 177

7 Why don't you put a pause before md ? Also, don't just run the .bat file by doubleclicking it: open cmd and call your bat from there. No window to close there!
– Sebastian Jun 14 '13 at 23:55

6 As for the syntax error - your ( is misplaced. It must be on the same line as your IF statement with a space before it. – dbenham Jun 15 '13 at 1:09

Thanks for pointing out the error :) –  Meow Jun 15 '13 at 15:45

5 Answers Active Oldest Votes

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

63 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.
+125

Share Improve this answer Follow edited Mar 20 '18 at 14:43 answered Nov 26 '15 at 1:37
Klitos Kyriacou
8,812 2 34 62

This doesn't seem to work in Windows 7. It spawns recursive subprocesses until something fails. Anyone have any corrections? – Bodey Baker Jan 6 '16 at 4:20

3 @porcoesphino Can you try if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit ) – Klitos Kyriacou Jan 6 '16 at 10:29

Oh wow. The space before the bracket was the issue. Does that also work in earlier versions? If so, it's probably worth updating your answer. Thanks. – Bodey Baker Jan 7
'16 at 0:43

@porcoesphino cmd never ceases to surprise me. Actually, I should say cmd keeps revealing new quirks all the time so it never surprises me any more. I'll update my
answer in a minute. – Klitos Kyriacou Jan 7 '16 at 0:49

'defined' is not recognized as an internal or external command? I don't know what's wrong on win7 – SLdragon Mar 19 '18 at 8:36

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

36 Share Improve this answer Follow answered Jun 15 '13 at 0:00


AMissico
21.1k 6 73 106

2 cmd /k myfile.bat – Diego Vieira Apr 15 '20 at 16:57

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

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

Share Improve this answer Follow edited Jul 17 '19 at 5:39 community wiki

https://stackoverflow.com/questions/17118846/how-to-prevent-batch-window-from-closing-when-error-occurs/33929280 1/2
12/2/2021 How to prevent batch window from closing when error occurs? - Stack Overflow
2 revs
JinSnow

This was so easy. No need to modify my script, and it shows the last lines it failed on. Thanks for the suggestion! – gamingexpert13 Jul 15 '19 at 20:41

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.
1
Share Improve this answer Follow answered Jul 16 '15 at 21:27
Darth Tater
387 3 7

1 this worked out great for me! It was a fleeting message and I did not have control over script invocation - this was the best option. – N K Aug 28 '16 at 3:00

How to prevent batch window from closing when error occurs?

0
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

Share Improve this answer Follow answered Dec 7 at 14:30


user2029101
76 8

https://stackoverflow.com/questions/17118846/how-to-prevent-batch-window-from-closing-when-error-occurs/33929280 2/2

You might also like