1

Is there a way to close the command prompt after a command line program ends?

I have an Electron app that I am starting with a batch file with the following commands:

start npm start

After exiting the Electron app, the command prompt remains open. I've tried adding exit to the end of the batch file, but this does not work.

1 Answers1

1

I would recommend implementing some goto placeholders in your batch file so that your script can follow a well defined path or workflow.

Similar to the answer here... Batch - If, ElseIf, Else

If you need to run a command initiated from the batch file you can use the /C flag to close the new window once the command is completed.

See this answer here... BAT file: Open new cmd window and enter code in there


Also...Here's an example that I had lying around which allows for user input if you require multiple tasks within the batch script. Included are two examples with the /k flag that allows the windows to remain open, and the /C flag that closes the initial command prompt.

@echo off
:start
cls
@echo Select An Action:
@echo.
@echo 1= Task 1 (new cmd /C, start npm -l)
@echo 2= Task 2 (new cmd /k, start npm -l)
@echo.
set /p userinp=Enter Your Choice:
set userinp=%userinp:~0,1%
if "%userinp%"=="1" goto 1
if "%userinp%"=="2" goto 2
if "%userinp%"=="3" goto 3
if "%userinp%"=="4" goto 4

if not "%userinp%" =="" @echo Invalid Choice
pause
goto start

:1
start cmd /C start npm -l
@echo Task 2 Complete...
@echo.
@echo Closing in 10 seconds...
timeout /t 10
goto end

:2
start cmd /k start npm -l
@echo Task 2 Complete...
@echo.
@echo Closing in 10 seconds...
timeout /t 10
goto end

:end 
exit
BugZap
  • 86
  • 4
  • Thanks for this. Unfortunately, the addition of the `/C` flag causes this script to close immediately and also terminates the running application. – Christopher Murphy Sep 13 '17 at 22:36
  • Would it be better to use the `/k` flag in your case rather than `/C`? – BugZap Sep 13 '17 at 22:40
  • Same issue. When I run the batch file, a program starts until it is terminated by the user, at which point the command prompt window that was created by the batch file remains open. Immediately killing the window with `/k` or `/C` also kills the process that should remain open. – Christopher Murphy Sep 13 '17 at 22:49
  • @ChristopherMurphy: I decided to install nodejs to see if I could get a better idea of what you're trying to accomplish. I don't think the option with the `/k` flag is the one you are after. See my edits in the original as I've included examples for both. The first option I'm able to keep the new window open and list npm usage info while closing the initial window after a 10 second timeout. – BugZap Sep 13 '17 at 23:32