67

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
2
  • 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!
    – 8192K
    Commented Jun 14, 2013 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
    Commented Jun 15, 2013 at 1:09

6 Answers 6

83
+125

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.

7
  • This doesn't seem to work in Windows 7. It spawns recursive subprocesses until something fails. Anyone have any corrections? Commented Jan 6, 2016 at 4:20
  • 4
    @porcoesphino Can you try if not defined in_subprocess (cmd /k set in_subprocess=y ^& %0 %*) & exit ) Commented Jan 6, 2016 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. Commented Jan 7, 2016 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. Commented Jan 7, 2016 at 0:49
  • 'defined' is not recognized as an internal or external command? I don't know what's wrong on win7
    – SLdragon
    Commented Mar 19, 2018 at 8:36
44

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

3
  • 5
    cmd /k myfile.bat Commented Apr 15, 2020 at 16:57
  • in my case i needed this for debugging a nuitka build and this gave me a hint .\app.exe
    – 3V1LXD
    Commented Jan 28, 2023 at 3:07
  • I tried using this as follows: cmd /k env\Scripts\activate && python fedex_main.py --mode "draft" && pause - however that does not seem to work. I can get it working by creating a new *.bat file, e.g. python_env_file.bat with the env\Scripts\activate && python fedex_main.py --mode "draft" && pause portion, then in a separate call cmd /k python_env_file.bat - but that seems overly convoluted vs. having everything in 1 file?
    – mfcss
    Commented Feb 3, 2023 at 8:05
9

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

2
  • This was so easy. No need to modify my script, and it shows the last lines it failed on. Thanks for the suggestion! Commented Jul 15, 2019 at 20:41
  • 1
    TIL: Drag and drop to the CMD window is a thing. Nice timesaver, thanks!
    – TimHayes
    Commented Aug 2, 2022 at 11:15
1

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
  • 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
    Commented Aug 28, 2016 at 3:00
1

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

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.