windows 如何防止执行批处理文件后控制台自动关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/988403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to prevent auto-closing of console after the execution of batch file
提问by joe
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?
我可以在批处理文件的末尾放置什么命令以防止在执行文件后自动关闭控制台?
回答by Dave Bauman
In Windows/DOS batch files:
在 Windows/DOS 批处理文件中:
pause
This prints a nice "Press any key to continue . . . "
message
这会打印一条不错的"Press any key to continue . . . "
消息
Or, if you don't want the "Press any key to continue . . ."
message, do this instead:
或者,如果您不想要该"Press any key to continue . . ."
消息,请改为执行以下操作:
pause >nul
回答by Rutger Nijlunsing
Depends on the exact question!
取决于确切的问题!
Normally pause
does the job within a .batfile.
通常pause
在.bat文件中完成这项工作。
If you want cmd.exenot to close to be able to remain typing, use cmd /k
command at the end of the file.
如果您不希望cmd.exe关闭以继续输入,请cmd /k
在文件末尾使用命令。
回答by Valentine Stone
If you want cmd.exe to not close, and able to continue to type, use
cmd /k
如果您希望 cmd.exe 不关闭,并且能够继续键入,请使用
cmd /k
Just felt the need to clarify what /k
does (from windows website):
只是觉得有必要澄清一下/k
(来自 Windows 网站):
/k
: Carries out the command specified by string and continues.
/k
: 执行字符串指定的命令并继续。
So cmd /k
without follow up command at the end of bat file will just keep cmd.exe
window open for further use.
因此cmd /k
,在 bat 文件末尾没有后续命令只会保持cmd.exe
窗口打开以供进一步使用。
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
另一方面pause
,在批处理文件的末尾将简单地暂停进程并cmd.exe
在第一次按下按钮时终止
回答by user6928445
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
回答by Andrey Hartung
If you are using Mavenand 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'.
如果您正在使用Maven并且您想跳过键入并防止控制台关闭以查看结果,则您需要使用CALL
脚本中的命令,除了“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.
如果您不使用该CALL
命令,过去的示例都将不起作用。
回答by lyfing
Add cmd.exe
as a new line below the code you want to execute:
cmd.exe
在要执行的代码下方添加新行:
c:\Python27\python D:\code\simple_http_server.py
cmd.exe
回答by Erich Chen
my way is to write an actual batch (saying "foo.bat") to finish the job; then create another "start.bat":
我的方法是编写一个实际的批处理(说“foo.bat”)来完成工作;然后创建另一个“start.bat”:
@echo off
cmd /k foo.bat
I find this is extremely useful when I set up one-time environment variables.
我发现这在我设置一次性环境变量时非常有用。
回答by user4155296
Call cmd
at the end of the batch file.
cmd
在批处理文件的末尾调用。
回答by npocmaka
besides pause
.
此外 pause
。
set /p=
can be used .It will expect user input and will release the flow when enter is pressed.
可以使用。它将期待用户输入,并在按下 Enter 时释放流程。
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.
除了用户输入中的任何内容都不会显示也不会保留在命令历史记录中之外,这将执行相同的操作。
回答by BlazeLP
Possibility 1: Just make 2 .bat files and write into the first:
可能性 1:只需制作 2 个 .bat 文件并写入第一个:
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)
批处理文件 2 是最终不会关闭的文件。因此,现在当您打开批处理 nr.1 时,它将启动第二个并自行关闭。当第二个完成时,它不会完全关闭(只要你不把 exit 放在最后)
Possibility 2: Batch file 1:
可能性 2:批处理文件 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
当第二个文件结束时,它将再次进入文件 1 并输出它的其余部分。有了它,您甚至可以制作错误处理程序。如果 nr.1 崩溃,它会进入 nr.2 并显示它