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

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

  1. “我的批处理文件”> 2。“其他一些批处理文件”

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

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

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

平台: Windows7


更新1

修改了结构,像这样的简单例子不能很好地工作, 只有一个批处理文件,即没有2。“其他批处理文件” 唯一的批处理文件包含这样的 smth

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

更新2

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

有什么办法能让控制台窗口一直开着吗?

257492 次浏览

If that is really all the batch file is doing, remove the cmd /K and add 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.

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.

  1. First, pin a shortcut for CMD.EXE to 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.EXE and 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.

For your purposes, you can either:

  1. Use /C and put a PAUSE at the end of your batch file.

    OR

  2. Change the command line to use /K and remove the PAUSE from your batch file.

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

pause >nul

At here:

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

Take a look what are you doing:

  1. (cmd /K) Start a NEW cmd instance.
  2. (& pause) Pause the CURRENT cmd instance.

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

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

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

' & pause'

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

I just press enter and type Pause and it works fine

You can just put a pause command in the last line of your batch file:

@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.

put at the end it will reopen your console

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