How To Prevent Batch Window From Closing When Error Occurs - Stack Overflow
How To Prevent Batch Window From Closing When Error Occurs - Stack Overflow
How To Prevent Batch Window From Closing When Error Occurs - Stack Overflow
- Stack Overflow
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
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
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.
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
0
I had the problem when using robocopy. My solution was:
https://stackoverflow.com/questions/17118846/how-to-prevent-batch-window-from-closing-when-error-occurs/33929280 2/2