How can I keep using the command prompt after executing a .cmd file in Windows? - cmd

After I double clicked a .cmd file and it executed successfully, it just closed the command prompt window. Even if I add pause to the end it also closes after I hit any key. So is there a way to let me keep using the command prompt, just as if I got the window from running cmd?

very simple.
Add the following line to the end of your code:
start /b cmd

Related

How to prevent batch file (.bat) from closing terminal when running commands?

On a Windows 7 machine if I run a PHPUnit Selenium command like this manually in the terminal:
phpunit --verbose --log-junit _selenium_tests\results\home.xml _selenium_tests\frontend\home.php
It spawns a browser and runs the test just fine. Then it outputs the following on the screen:
Time: 10 seconds, Memory: 3.50Mb
OK (1 test, 3 assertions)
And the terminal stays open.
Now if I copy and paste the exact command in an empty file and save it as test.bat and click it, it also runs the test. I can see the browser open and all tests run. Only problem is it closes the terminal prompt right after. So I can't see the above output.
An even bigger problem is, since it closes the terminal if I add more commands for other tests after that initial one they don't run.
I tried adding:
pause
at the end of the bat file but no luck, it still closes. Any idea how to prevent this and be able to run one command after another without the terminal ever closing?
Your question is similar to this one. Try using call in front of your command. If you run a .bat file from another .bat file and don't use call, control doesn't return to the first batch file, so pause doesn't get executed.
Try cmd /K phpunit --verbose --log-junit _selenium_tests\results\home.xml _selenium_tests\frontend\home.php
The /K option in cmd /K string Carries out the command specified by string but remains,see http://www.computerhope.com/cmd.htm
Also, I don't know the file type of the phpunit command you execute - I'm not familiar with selenium. If it is batch file (i.e. ends with .bat), you just can't call them from another batch file: everything below the call to the second batch file will never get executed.
You then need to use the CALL command. CALL Enables a user to execute a batch file from within another batch file, see http://www.computerhope.com/call.htm

What is a batch file command to open a cmd window?

Yes, I tried googling this, by the way.
Basically, what is the dos command that will open a dos command window?
In other words, if you have a .bat file full of script commands, what command can you do that will open a command window?
You can try this maybe. In the command line it starts it so I assume in a bat file it'll do the same.
start cmd
If you want it to run another bat or something you can do:
start script.bat
If you want it to close then:
start cmd /c script.bat

How to close the command prompt when running a batch file in ruby in windows

am opening command prompt using system command like system("start cmd.exe") and then running some batch file. I want to stop the batch file running in command prompt or close the command prompt window. Could you please help me with this.
If you want the command prompt to terminate you just write the following in your script..:
system("exit")
To stop the batch file I will use win32ole lib and send a CTRL+C combo like this..:
require "win32ole"
key = WIN32OLE.new('Wscript.Shell')
key.SendKeys('{^}C{ENTER}')

How to return an error code from a process that runs in another cmd window (windows)

I'm running a script from the cmd prompt. This script opens another cmd prompt and runs another batch file there. I want to wait for the error code and then send it back to the original cmd window. Is there a nice way to do this without writing the error code to a file?
Thanks,
Li
If I inderstand you correctly, you want this solution. It solves the problem of returning error level to the calling script from the script that was run in a separate cmd session.

How to prevent auto-closing of console after the execution of batch file

What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?
In Windows/DOS batch files:
pause
This prints a nice "Press any key to continue . . . " message
Or, if you don't want the "Press any key to continue . . ." message, do this instead:
pause >nul
Depends on the exact question!
Normally pause does the job within a .bat file.
If you want cmd.exe not to close to be able to remain typing, use cmd /k command at the end of the file.
If you want cmd.exe to not close, and able to continue to type, use cmd /k
Just felt the need to clarify what /k does (from windows website):
/k : Carries out the command specified by string and continues.
So cmd /k without follow up command at the end of bat file will just keep cmd.exe window open for further use.
On the other hand pause at the end of a batch file will simply pause the process and terminate cmd.exe on first button press
If you are using Maven and you want to skip the typing and prevent the console from close to see the result you need to use the CALL command in the script, besides just the 'mvn clean install'.
Like this will close the console
ECHO This is the wrong exemple
mvn clean install
pause
Like this the console will stay open
ECHO This is the right exemple
CALL mvn clean install
pause
If you dont use the CALL command neither of the pasts exemples will work. Because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe
The below way of having commands in a batch file will open new command prompt windows and the new windows will not exit automatically.
start "title" call abcd.exe param1 param2
start "title" call xyz.exe param1 param2
Add cmd.exe as a new line below the code you want to execute:
c:\Python27\python D:\code\simple_http_server.py
cmd.exe
my way is to write an actual batch (saying "foo.bat") to finish the job; then create another "start.bat":
#echo off
cmd /k foo.bat
I find this is extremely useful when I set up one-time environment variables.
Call cmd at the end of the batch file.
Had problems with the answers here, so I came up with this, which works for me (TM):
cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause
besides pause.
set /p=
can be used .It will expect user input and will release the flow when enter is pressed.
or
runas /user:# "" >nul 2>&1
which will do the same except nothing from the user input will be displayed nor will remain in the command history.
This little hack asks the user to enter a key and stores it into the variable %exitkey% (this variable could be called anything you like though).
set /p exitkey= "Press any key to continue..."
NB: the space after the '=' is very important
I know I'm late but my preferred way is:
:programend
pause>nul
GOTO programend
In this way the user cannot exit using enter.
Possibility 1:
Just make 2 .bat files and write into the first:
start <filename> // name of 2nd batch file
exit
Batch file 2 is the file that wont close in the end.
So now when you open batch nr.1 It will start the 2nd and cloe itself.
When the 2nd finishes it will not close entirely (as long as you wont put exit at the end)
Possibility 2:
Batch file 1:
call <filename>
cls
echo End of file
pause
<any code you want>
When the 2nd file ends then it will proceed to file 1 again and output the rest of it. With that you can even make error handlers. If nr.1 crashes it goes into nr.2 and displays it
There are two ways to do it depend on use case
1) If you want Windows cmd prompt to stay open so that you can see execution result and close it afterwards; use
pause
2) if you want Windows cmd prompt to stay open and allow you to execute some command afterwords; use
cmd
pause
or
echo text to display
pause>nul
Easy, add cmd to your last line of bat, BUT! if you reset or clear your system path, you must start your cmd with the full path, like:
%windir%\system32\cmd.exe
For example, I have a bat file to reset jdk to old version like this:
PATH=C:\Program Files\Java\jdk1.6.0_45\bin;C:\apache-ant-1.7.1\bin
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45
%windir%\system32\cmd.exe
since I reset the system path, I have to run cmd with the full path, or the system can't find cmd.exe, it will fail to run cmd, and just close the window, and you can't see the error msg.
Run the .exe file and then pause the cmd
batch script example :
#echo off
myProgram.exe
PAUSE
batch script example with arguments :
#echo off
myProgram.exe argumentExample1 argumentExample2
PAUSE
I added #echo off because I don't want to show C:\user\Desktop>myProgram.exe and C:\user\Desktop>PAUSE in the cmd
cmd /k cd C:\Projects.....
If you want your cmd opened at specific long location
add pause (if you don't want anything else to show up add) >nul it should look like:#echo offtitle niceecho hellopause >nulall you will see is "hello"
I personally put pause >nul and it waits for a key to be pressed without showing any extra text in the console.
using : call yourbatch.cmd
does the job
will process the script and then continue ejecuting other code on same window (cmd instance)

Resources