Batch files : How to leave the console window open

WindowsBatch File

Windows Problem Overview


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

  1. "My Batch File" > 2. "Some Other Batch File"

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 do I 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.

Platform : Windows7


UPDATE 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

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

UPDATE 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?

Windows Solutions


Solution 1 - Windows

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.

Solution 2 - Windows

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"

Solution 3 - Windows

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

Solution 4 - Windows

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

pause >nul

Solution 5 - Windows

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

' & pause'

Solution 6 - Windows

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.

Solution 7 - Windows

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

Solution 8 - Windows

put at the end it will reopen your console

start cmd 

Solution 9 - Windows

I just press enter and type Pause and it works fine

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeimusView Question on Stackoverflow
Solution 1 - WindowsaphoriaView Answer on Stackoverflow
Solution 2 - WindowsElektroStudiosView Answer on Stackoverflow
Solution 3 - WindowsSaiView Answer on Stackoverflow
Solution 4 - WindowsBali CView Answer on Stackoverflow
Solution 5 - WindowsUmbertoView Answer on Stackoverflow
Solution 6 - WindowsPatrick HillertView Answer on Stackoverflow
Solution 7 - WindowsDavid CastroView Answer on Stackoverflow
Solution 8 - WindowsNikView Answer on Stackoverflow
Solution 9 - WindowsJfly 27View Answer on Stackoverflow