windows 批处理文件:如何让控制台窗口保持打开状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13746177/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:06:34  来源:igfitidea点击:

Batch files : How to leave the console window open

windowsbatch-file

提问by deimus

I have two batch files, one of them executes another, i.e.

我有两个批处理文件,其中一个执行另一个,即

  1. "My Batch File" > 2. "Some Other Batch File"
  1. “我的批处理文件”> 2.“其他一些批处理文件”

I've created a shortcut of the first batch file and edited its properties to call its in following way.

我已经创建了第一个批处理文件的快捷方式并编辑了它的属性以按以下方式调用它。

cmd.exe /k "<SomePath>\<My Batch File>.bat" & pause

What i want to doI want the console window to be open after the execution of the batch file is over. Now it just closes, tried to play around the cmd flags, no result.

我想要做什么 我希望在批处理文件执行结束后打开控制台窗口。现在它刚刚关闭,试图绕过 cmd 标志,但没有结果。

Platform : Windows7

平台:Windows7



UPDATE 1

更新 1

Modified the structure, the simple example like this does not work as well, Only one batch file i.e. there is no the 2. "Some Other Batch File" The only batch file contains smth like this

修改了结构,像这样的简单例子不行,只有一个批处理文件,即没有 2.“Some Other Batch File” 唯一的批处理文件包含这样的smth

start /B /LOW /WAIT make package
cmd /K


UPDATE 2

更新 2

The same shortcut which is invoked from Explorer does not close the console window. But the console window closes when the shortcut is invoked from the pinned item on taskbar

从资源管理器调用的相同快捷方式不会关闭控制台窗口。 但是当从任务栏上的固定项目调用快捷方式时,控制台窗口会关闭

Any ideas how to keep the console window open?

任何想法如何保持控制台窗口打开?

回答by aphoria

If that is really all the batch file is doing, remove the cmd /Kand add PAUSE.

如果这真的是批处理文件所做的一切,请删除cmd /K并添加PAUSE.

start /B /LOW /WAIT make package
PAUSE

Then, just point your shortcut to "My Batch File.bat"...no need to run it with CMD /K.

然后,只需将您的快捷方式指向"My Batch File.bat"...无需使用CMD /K.

UPDATE

更新

Ah, some new info...you're trying to do it from a pinned shortcut on the taskbar.

啊,一些新信息……您正在尝试从任务栏上的固定快捷方式中执行此操作。

I found this, Adding Batch Files to Windows 7 Taskbar like the Vista/XP Quick Launch, with the relevant part below.

我发现了这一点,将批处理文件添加到 Windows 7 任务栏,如 Vista/XP 快速启动,相关部分如下。

  1. First, pin a shortcut for CMD.EXEto the taskbar by hitting the start button, then type "cmd" in the search box, right-click the result and chose "Pin to Taskbar".
  2. Right-click the shortcut on the taskbar.
  3. You will see a list that includes "Command Prompt" and "Unpin this program from the taskbar".
  4. Right-click the icon for CMD.EXEand select Properties.
  5. In the box for Target, go to the end of "%SystemRoot%\system32\cmd.exe"and type " /C "and the path and name of the batch file.
  1. 首先,CMD.EXE通过点击开始按钮将快捷方式固定到任务栏,然后在搜索框中键入“cmd”,右键单击结果并选择“固定到任务栏”。
  2. 右键单击任务栏上的快捷方式。
  3. 您将看到一个列表,其中包括“命令提示符”和“从任务栏取消固定此程序”。
  4. 右键单击 的图标CMD.EXE并选择Properties
  5. 在 Target 框中,转到末尾"%SystemRoot%\system32\cmd.exe"并键入" /C "批处理文件的路径和名称。

For your purposes, you can either:

为了您的目的,您可以:

  1. Use /Cand put a PAUSEat the end of your batch file.

    OR

  2. Change the command line to use /Kand remove the PAUSEfrom your batch file.
  1. 使用/C并将 aPAUSE放在批处理文件的末尾。

    或者

  2. 更改命令行以使用/KPAUSE从批处理文件中删除。

回答by ElektroStudios

At here:

在这里:

cmd.exe /k "<SomePath>\<My Batch File>.bat" & pause

Take a look what are you doing:

看看你在做什么:

  1. (cmd /K) Start a NEWcmd instance.
  2. (& pause) Pause the CURRENTcmd instance.
  1. (cmd /K) 启动一个新的cmd 实例。
  2. (& pause) 暂停CURRENTcmd 实例。

How to resolve it? well,using the correct syntax, enclosing the argument for the new CMD instance:

如何解决?好吧,使用正确的语法,包含新 CMD 实例的参数:

cmd.exe /k ""<SomePath>\<My Batch File>.bat" & pause"

回答by Sai

I just written last line as Pauseit worked fine with both .bat and .cmd. It will display message also as 'Press any key to continue'.

我刚刚将最后一行写为Pause,它适用于 .bat 和 .cmd。它还将显示消息“按任意键继续”。

回答by Bali C

In the last line of the batch file that you want to keep open put a

在要保持打开的批处理文件的最后一行中放置一个

pause >nul

pause >nul

回答by Umberto

For leaving the console window open you only have to add to the last command line in the batch file:

要让控制台窗口保持打开状态,您只需添加到批处理文件中的最后一个命令行:

' & pause'

回答by Patrick Hillert

You can just put a pausecommandin the last line of your batch file:

您可以将pause命令放在批处理文件的最后一行:

@echo off
echo Hey, I'm just doing some work for you.
pause

Will give you something like this as output:

会给你这样的东西作为输出:

Hey, I'm just doing some work for you.

Press any key to continue ...

嘿,我只是为你做一些工作。

按任意键继续 ...

Note:Using the @echo prevents to output the command before the output is printed.

注意:使用@echo 可以防止在打印输出之前输出命令。

回答by David Castro

rem Just use "pause" at the end of the batch file.
...
......
.......
pause

回答by Nik

put at the end it will reopen your console

放在最后它会重新打开你的控制台

start cmd 

回答by Jfly 27

I just press enter and type Pause and it works fine

我只需按 Enter 并输入 Pause,它就可以正常工作