问题
I simply wish to leave a cmd.exe window open upon completion of a batch file like the following. I am test launching it from Windows 7 Explorer, with cmd.exe version 6.1.7601. Beeps and pause work fine, but I want to leave the cmd open to issue subsequent commands.
@echo off
:: wschloss Jul 2015
:: This batch runs if the laptop's Core Temp is close to critical
:: as determined by the "Core Temp" utility at
:: "C:\ORION\System Information\CoreTemp\64bit\Core Temp.exe"
:: http://www.alcpu.com/CoreTemp/
::
color 1F :: White on Blue
:: Beeps!
echo .
type c:\bill\beep.txt :: causes the Laptop to beep for attention
cls
::
echo .
echo Laptop Critical Temperature Approaches!
echo .
echo Save Your Data and Cool me down NOW!
echo .
pause
color
exit /B
:: EOP
This seems trivial but I am obviously missing something. Thanks for any help.
回答1:
Add cmd /k
to the end of your file to redirect the program to Command Prompt. (It will end your program, then, in the same window, open command prompt)
@echo off
:: wschloss Jul 2015
:: This batch runs if the laptop's Core Temp is close to critical
:: as determined by the "Core Temp" utility at
:: "C:\ORION\System Information\CoreTemp\64bit\Core Temp.exe"
:: http://www.alcpu.com/CoreTemp/
::
color 1F :: White on Blue
:: Beeps!
echo .
type c:\bill\beep.txt :: causes the Laptop to beep for attention
cls
::
echo .
echo Laptop Critical Temperature Approaches!
echo .
echo Save Your Data and Cool me down NOW!
echo .
pause
color
cmd /k
:: EOP
That, and if you want the previous output cleared, you could of course add cls
right before the cmd /k
.
回答2:
Use cmd /k yourbatchfile.bat
command line, then exit /b
will work as you want it to.
回答3:
Normally I open up a command window first, and then run my batch scripts from there, so my window does not close when the script ends.
If I want to be able to double click a script and have it run and the window remain open, then I create a shortcut that runs cmd /k "myscript.bat"
.
But it sounds like you don't want to do either of the above. You could start a new CMD shell with the /K option within the same window using START /B. Your parent CMD shell will terminate when the script completes, but the child CMD shell will remain so the window will remain open.
@echo off
:: wschloss Jul 2015
:: This batch runs if the laptop's Core Temp is close to critical
:: as determined by the "Core Temp" utility at
:: "C:\ORION\System Information\CoreTemp\64bit\Core Temp.exe"
:: http://www.alcpu.com/CoreTemp/
::
color 1F :: White on Blue
:: Beeps!
echo .
type c:\bill\beep.txt :: causes the Laptop to beep for attention
cls
::
echo .
echo Laptop Critical Temperature Approaches!
echo .
echo Save Your Data and Cool me down NOW!
echo .
pause
color
start /b cmd /k
exit /B
:: EOP
来源:https://stackoverflow.com/questions/31413540/batch-file-exit-b