简体   繁体   English

BAT文件执行后保持CMD打开

[英]Keep CMD open after BAT file executes

I have a bat file like this:我有一个这样的bat文件:

ipconfig

That will print out the IP info to the screen, but before the user can read that info CMD closes itself.这会将 IP 信息打印到屏幕上,但在用户可以读取该信息之前,CMD 会自行关闭。

I believe that CMD assumes the script has finished, so it closes.我相信 CMD 假设脚本已经完成,所以它会关闭。

How do I keep CMD open after the script is finished?脚本完成后如何保持CMD打开?

在 .BAT 文件的末尾pause

Depending on how you are running the command, you can put /k after cmd to keep the window open.根据您运行命令的方式,您可以在cmd后放置/k以保持窗口打开。

cmd /k my_script.bat

Simply adding cmd /k to the end of your batch file will work too.只需将cmd /k添加到批处理文件的末尾也可以。 Credit to Luigi D'Amico who posted about this in the comments below.感谢Luigi D'Amico ,他在下面的评论中对此发表了评论。

Just add @pause at the end.只需在最后添加@pause

Example:例子:

@echo off
ipconfig
@pause

Or you can also use:或者你也可以使用:

cmd /k ipconfig

When the .bat file is started not from within the command line (eg double-clicking).当 .bat 文件不是从命令行中启动时(例如双击)。

echo The echoed text
@pause

在_暂停

echo The echoed text
pause

暂停

echo The echoed text
cmd /k

命令行

echo The echoed text & pause

and_pause

Adding pause in (Windows 7) to the end did not work for me在(Windows 7)中添加pause对我不起作用
but adding the cmd /k in front of my command did work.但是在我的命令前面添加cmd /k确实有效。

Example :示例:

cmd /k gradlew cleanEclipse

start cmd /k did the magic for me. start cmd /k为我做了魔法。 I actually used it for preparing cordova phonegap app it runs the command, shows the result and waits for the user to close it.我实际上用它来准备cordova phonegap应用程序,它运行命令,显示结果并等待用户关闭它。 Below is the simple example下面是一个简单的例子

start cmd /k echo Hello, World!

What I did use in my case我在我的案例中使用了什么

start cmd /k cordova prepare

Update更新

You could even have a title for this by using你甚至可以使用这个标题

start "My Title" echo Hello, World!

如果您在命令行中启动脚本,则添加exit /b以保持 CMD 打开

在 Windows 中,将“& Pause”添加到文件中命令的末尾。

I was also confused as to why we're adding a cmd at the beginning and I was wondering if I had to open the command prompt first.我也很困惑为什么我们要在开头添加一个cmd ,我想知道是否必须先打开命令提示符。 What you need to do is type the full command along with cmd /k .您需要做的是键入完整命令和cmd /k For example assume your batch file name is "my_command.bat" which runs the command javac my_code.java then the code in your batch file should be:例如,假设您的批处理文件名是“my_command.bat”,它运行命令javac my_code.java那么批处理文件中的代码应该是:

cmd /k javac my_code.java

So basically there is no need to open command prompt at the current folder and type the above command but you can save this code directly in your batch file and execute it directly.所以基本上不需要在当前文件夹打开命令提示符并键入上述命令,但您可以将此代码直接保存在批处理文件中并直接执行。

javac -d C:\xxx\lib\ -classpath C:\xxx\lib\ *.java

cmd cd C:\xxx\yourbat.bat

the second command make your cmd window not be closed.第二个命令使您的 cmd 窗口不会被关闭。 The important thing is you still able to input new command重要的是你仍然可以输入新的命令

As a sidenote this also works when running a command directly from the search bar in windows.作为旁注,这也适用于直接从 Windows 中的搜索栏运行命令。

eg directly running ipconfig will directly close the cmd window after the command has exited.例如直接运行ipconfig会在命令退出后直接关闭 cmd 窗口。

在此处输入图片说明

Using cmd \\k <command> won't - which was what i was trying to do when i found this answer.使用cmd \\k <command>不会 - 当我找到这个答案时,这就是我想要做的。

在此处输入图片说明

It has the added advantage of always recognizing the command you're trying to run.它具有始终识别您尝试运行的命令的附加优势。 Eg running echo hello world from the searchbar won't work because that is not a command, however cmd \\k echo hello world works just fine.例如,从搜索栏运行echo hello world将不起作用,因为这不是命令,但是cmd \\k echo hello world工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM