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?
我可以在批处理文件的末尾放置什么命令来防止在执行文件后自动关闭控制台?
16 个解决方案
#1
In Windows/DOS batch files:
在Windows / DOS批处理文件中:
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
#2
Depends on the exact question!
取决于确切的问题!
Normally pause
does the job within a .bat file.
通常暂停在.bat文件中执行作业。
If you want cmd.exe not to close to be able to remain typing, use cmd /k
command at the end of the file.
如果您希望cmd.exe不能关闭以便能够保持键入,请在文件末尾使用cmd / k命令。
#3
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:执行string指定的命令并继续。
So cmd /k
without follow up command at the end of bat file will just keep cmd.exe
window open for further use.
因此,在bat文件末尾没有跟进命令的cmd / k只会保持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
另一方面,批处理文件末尾的暂停只会暂停进程并在第一次按下按钮时终止cmd.exe
#4
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
#5
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
#6
Call cmd
at the end of the batch file.
在批处理文件的末尾调用cmd。
#7
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.
我发现在设置一次性环境变量时这非常有用。
#8
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.
除了用户输入之外的任何内容都将显示,也将保留在命令历史记录中。
#9
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它将开始第二次和cloe本身。当第二次完成时它不会完全关闭(只要你不会把出口放在最后)
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并显示它
#10
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'.
如果您正在使用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命令,那么过去的例子都不会起作用。
#11
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).
这个小黑客要求用户输入一个密钥并将其存储到变量%exitkey%中(尽管这个变量可以被调用)。
set /p exitkey= "Press any key to continue..."
NB: the space after the '=' is very important
注意:'='后面的空格非常重要
#12
pause
or
echo text to display
pause>nul
#13
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
1)如果您希望Windows cmd提示保持打开状态,以便您可以看到执行结果并在之后关闭它;使用
pause
2) if you want Windows cmd prompt to stay open and allow you to execute some command afterwords; use
2)如果您希望Windows cmd提示保持打开状态并允许您执行一些命令;使用
cmd
#14
Had problems with the answers here, so I came up with this, which works for me (TM):
这里的答案有问题,所以我想出了这个,这适用于我(TM):
cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause
#15
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:
很容易,添加cmd到你的最后一行蝙蝠,但是!如果重置或清除系统路径,则必须使用完整路径启动cmd,例如:
%windir%\system32\cmd.exe
For example, I have a bat file to reset jdk to old version like this:
例如,我有一个bat文件将jdk重置为旧版本,如下所示:
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.
因为我重置系统路径,我必须用完整路径运行cmd,否则系统找不到cmd.exe,它将无法运行cmd,只是关闭窗口,你就看不到错误信息。
#16
I know I'm late but my preferred way is:
我知道我迟到但我的首选方式是:
:programend
pause>nul
GOTO programend
In this way the user cannot exit using enter.
这样,用户无法使用enter退出。
#1
In Windows/DOS batch files:
在Windows / DOS批处理文件中:
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
#2
Depends on the exact question!
取决于确切的问题!
Normally pause
does the job within a .bat file.
通常暂停在.bat文件中执行作业。
If you want cmd.exe not to close to be able to remain typing, use cmd /k
command at the end of the file.
如果您希望cmd.exe不能关闭以便能够保持键入,请在文件末尾使用cmd / k命令。
#3
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:执行string指定的命令并继续。
So cmd /k
without follow up command at the end of bat file will just keep cmd.exe
window open for further use.
因此,在bat文件末尾没有跟进命令的cmd / k只会保持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
另一方面,批处理文件末尾的暂停只会暂停进程并在第一次按下按钮时终止cmd.exe
#4
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
#5
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
#6
Call cmd
at the end of the batch file.
在批处理文件的末尾调用cmd。
#7
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.
我发现在设置一次性环境变量时这非常有用。
#8
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.
除了用户输入之外的任何内容都将显示,也将保留在命令历史记录中。
#9
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它将开始第二次和cloe本身。当第二次完成时它不会完全关闭(只要你不会把出口放在最后)
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并显示它
#10
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'.
如果您正在使用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命令,那么过去的例子都不会起作用。
#11
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).
这个小黑客要求用户输入一个密钥并将其存储到变量%exitkey%中(尽管这个变量可以被调用)。
set /p exitkey= "Press any key to continue..."
NB: the space after the '=' is very important
注意:'='后面的空格非常重要
#12
pause
or
echo text to display
pause>nul
#13
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
1)如果您希望Windows cmd提示保持打开状态,以便您可以看到执行结果并在之后关闭它;使用
pause
2) if you want Windows cmd prompt to stay open and allow you to execute some command afterwords; use
2)如果您希望Windows cmd提示保持打开状态并允许您执行一些命令;使用
cmd
#14
Had problems with the answers here, so I came up with this, which works for me (TM):
这里的答案有问题,所以我想出了这个,这适用于我(TM):
cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause
#15
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:
很容易,添加cmd到你的最后一行蝙蝠,但是!如果重置或清除系统路径,则必须使用完整路径启动cmd,例如:
%windir%\system32\cmd.exe
For example, I have a bat file to reset jdk to old version like this:
例如,我有一个bat文件将jdk重置为旧版本,如下所示:
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.
因为我重置系统路径,我必须用完整路径运行cmd,否则系统找不到cmd.exe,它将无法运行cmd,只是关闭窗口,你就看不到错误信息。
#16
I know I'm late but my preferred way is:
我知道我迟到但我的首选方式是:
:programend
pause>nul
GOTO programend
In this way the user cannot exit using enter.
这样,用户无法使用enter退出。