19

The application I want to start is MongoDB. If I would start it normally, it looks like this:

enter image description here

I don't like the amount of line breaks and I have a lot of screen space, so I would like to utilize said space to get rid of the line breaks.

I can change the size of the console window with MODE, so I wrote a batch file like this:

@ECHO OFF
MODE con:cols=140 lines=70
%~dp0mongodb\bin\mongod --dbpath %~dp0data --rest

So far, so good. When I start this batch file, I get a larger window, as desired.

But when I now press Ctrl+C to exit MongoDB, I get the annoying prompt:

Terminate batch job (Y/N)?

Which is useless, because the command I just exited out of was the last command in the batch job anyway and no matter what I answer, the result is the same.

So, how can I get a larger console window for the application without having that prompt when I hit Ctrl+C?

7 Answers 7

12

I spent a few hours today to implement a small C# application that can wrap another console application and adjust the window size of the console host.

console-wrapper is easy to use. It only expects a couple of command line parameters:

Usage: console-wrapper.exe [OPTIONS]

Options:

      --subject=VALUE        The application that should be started by the
                               console wrapper.
      --width=VALUE          The desired width of the console window.
      --height=VALUE         The desired height of the console window.
  -h, -?, --help             Shows this help message

--width and --height are used to set the size of the resulting console window. --subject can be used optionally, if it is omitted, the remaining parameters are treated as the command (with parameters) to start.

So the final call I'm using now in our startup script is:

START "MongoDB" database\console-wrapper.exe --width=140 --height=70 %~dp0database\mongodb\bin\mongod.exe --dbpath %~dp0database\data --rest

The resulting window will have the requested size and upon hitting Ctrl+C, the window will close (after having properly shut down the contained process).

0
6

As far as I understand the question, you want MongoDB to have the specified size of console window and to exit when you press Ctrl+C. Then the answer is pretty simple: use shortcut to start it.

I don't have mongod executable, so I would use perl.exe as an example. When you start it from Run dialog (Windows+R), the size of the console window would be standard 80×25 with buffer size for 300 lines.

  1. Create a shortcut to mongod (or perl in my case).
  2. Right-click the shortcut and click Properties.
    1. On Shortcut tab in Target field, add parameters to mongod.
    2. Start in field controls the initial current directory of the started program. By default it will be the directory where .exe is located.
  3. Click Layout tab.
    1. Change Window size to 140×70.
      Shortcut properties, Layout tab
    2. You can increase the Height of Screen buffer size to 1000 or more to be able to scroll up to older messages.
    3. If you clear Let system position window, you can specify the location of the console window on the screen.
  4. Click OK to close shortcut properties.

Now when you click this shortcut, the console window would be of the specified size. When you press Ctrl+C, the application exits and the window closes without any additional prompts.


You can also change the layout of the window after you launched it from Run dialog. Right-click the title of the console window and click Properties. Click Layout tab, adjust the window size and click OK to close the properties and to apply the changes immediately. (If the window was started from shortcut, the properties of the shortcut are updated.)

The next time you start the same application, the size of the console window will be magically the same size. (I haven't checked it this time but it used to work.)

4
  • The location of mongod.exe is not fixed. It will change depending on the deployment of the application. Which is why I was aiming for a solution through the console directly. When I put a relative path in the target or working directory fields for a shortcut, it won't accept them :( Oct 3, 2013 at 18:14
  • @OliverSalzburg Did you try the approach of re-starting the batch? I mean your batch starts itself via start cmd /k and passes an additional parameter or sets an environment variable. The second instance then changes console window size and starts mongod.exe. Oct 3, 2013 at 18:29
  • Yes, I tried a lot of things. But as long as there is a batch file involved, the solution will suffer from the Terminate batch job (Y/N)? prompt :\ I actually just finished implementing a wrapper application to solve this for me though. Oct 3, 2013 at 18:48
  • @OliverSalzburg It will be a very useful utility. I also thought a wrapper would be the only way… Oct 6, 2013 at 7:10
6

Have you considered running it from a powershell script? Powershell is capable of resizing it's own window

$h = Get-Host
$buf = $h.UI.RawUI.BufferSize
$buf.Width = 300
$h.ui.rawui.BufferSize = $buf
$win = $h.UI.RawUI.WindowSize
$win.Width = 150
$h.ui.rawui.WindowSize = $win
#Command to start mongodb here.

This creates a window with a size of 150 but also has a sideways scrollable buffer of 300.

1
  • Yeah, I did consider it, but didn't know how to implement it at the time. This works perfectly, except for the slight annoyance that execution of scripts might possible be disabled. Oct 3, 2013 at 18:46
3

The default console layout settings live here in the registry:

[HKEY_CURRENT_USER\Console]

You can modify these settings and they will apply to ALL command windows that launch, regardless of the path. Unfortunately, the raw values are not human friendly.

To set them the way you want them, open a regular cmd window, then edit the settings to your liking. Look in the registry at the above path and you should see a sub key under "Console" for cmd.exe. Export that key to a file, change the path to the above and import, or just manually copy the values from one to the other. You can then delete the cmd.exe specific entry. Now, all command windows that open will use the modified defaults.

I've found it handy to create a couple of registry entry files with my standard defaults so that I can easily pop them onto a new server when I RDP in for the first time. I made presets for the two sizes I typically use.

For 1024 x 768 (I use this for servers normally accessed via a multi session RDP tool):

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Console]
"ScreenBufferSize"=dword:012c0050
"WindowSize"=dword:00190050
"WindowPosition"=dword:000a000a
"FontSize"=dword:000c0007
"FontFamily"=dword:00000030
"FontWeight"=dword:00000190
"FaceName"="Terminal"
"QuickEdit"=dword:00000001

For 1280 x 1024:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Console]
"ScreenBufferSize"=dword:0bb800af
"WindowSize"=dword:004b00af
"WindowPosition"=dword:000a000a
"FontSize"=dword:000c0007
"FontFamily"=dword:00000030
"FontWeight"=dword:00000190
"FaceName"="Terminal"
"QuickEdit"=dword:00000001
2

All that's in the batch file may be passed to CMD on its command line. For instance:

CMD.exe /k "TITLE In my home: & MODE con:cols=140 & DIR %HOMEPATH% /od /w"

The ampersands serve as separators. And a shortcut created with something like this as the commandline should do the trick.

0

Put the Start command in front of calling mongodb. I believe that should fix it.

1
  • No, that opens MongoDB in a new console window. When I use start /b it will leave the console window open after I press Ctrl+C. Oct 2, 2013 at 16:25
0

If you are happy to run a batch file along with a couple of tiny helper programs, a complete solution is posted by me here:
How can a batch file run a program and set the position and size of the window?

Contents as below . . . .

This demo batch file will run a command-line program and set the position and size of its Command Prompt console window. The general theme of this batch is the same as the previous two solutions (at the Stack Overflow page in the link above), but there were some unique problems and workarounds to make this work -- this is a Command Prompt window hosted by 'CMD.EXE' and not a GUI window like 'EXPLORER.EXE'.

Read all the comments and the description in the batch file for more info.

Tools used:
1.  MonitorInfoView  by Nir Sofer (41 KB)
2.  NirCmd  by Nir Sofer (43 KB)
3.  GetPIDs  by Daniel Scheibli (280 KB)
4.  upx.exe  ---  is the command-line program we will use for this demo; place it in same directory as the batch file.
5.  A batch file  ---  see below

Gather all five files into a directory. This is the batch file, ready to run on any Windows system.

What this demo batch is going to do, apart from presenting you with a custom console window, is it will run upx.exe with the command-line switches --best -v -o getpids-compressed.exe getpids.exe. We are going to compress our tool 'GetPIDs' down to 88 KB ........After launching, double-click on the title bar of this console window to see an interesting phenomenon; this console window behaves like a GUI !

@echo off
setlocal enabledelayedexpansion enableextensions
pushd %~dp0%

REM ----- ADD SOME USEFUL INFORMATION TO THIS CONSOLE WINDOW TITLE.
for /f "tokens=3" %%A in ('getpids.exe') do set _PROCESS_ID_=%%A
title This batch file runs a command-line program and sets the position and size of its console window        (PROCESS ID = %_PROCESS_ID_%)

REM ********************** DESCRIPTION ************************************
REM ** This script opens a command-line program console window with specified
REM ** screen properties at the primary monitor (containing the taskbar).
REM ** The "X/Y position" and "W/H size" of the console window is auto-set by
REM ** this script and the monitor resolution is auto-calculated to suit.
REM ** 'MonitorInfoView.exe' is the helper tool used to capture the resolution
REM ** info of the monitor.
REM ** 'nircmd.exe' is the tool performing all the display trickery.
REM ** 'getpids.exe' is the helper tool used to capture the Process ID of the
REM ** working batch file, thus allowing us to identify which CMD.EXE host this
REM ** console window belongs to when looking at Task Manager.
REM **
REM ** To tweak this script, go to the code section named:
REM ** >>>>> USER INPUT/PREFERENCES ARE ALL SET HERE <<<<<
REM ***********************************************************************

REM ----- CLEAR ANY PREVIOUS JOB OUTPUTS IF THEY EXIST
if exist ~TMP.TXT type NUL > ~TMP.TXT

REM ----- OUTPUT THE PRIMARY MONITOR INFORMATION TO A TEXT FILE
MonitorInfoView.exe /hideinactivemonitors 1 /stext ~TMP.TXT

REM ----- ISOLATE THE RESOLUTION LINE, REMOVING ALL THE OTHER LINES IN THE TEXT FILE
for /f "delims=" %%A in ('type "~TMP.TXT" ^|find.exe /i "Maximum Resolution"') do echo %%A>~TMP.TXT

REM ----- GET THE RESOLUTION NUMBERS, AND SET THEM AS VARIABLES
for /f "tokens=3,4 delims=:X " %%A in ('type "~TMP.TXT"') do set _SCREENW_=%%A& set _SCREENH_=%%B


REM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
REM >>>>>>>>>> USER INPUT/PREFERENCES ARE ALL SET HERE [BEGIN] <<<<<<<<<<<<

REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN:  FILE NAME (WITH EXTENSION)
REM ----- |
REM ----- | |IF PROG TO BE RUN IS 'CMD.EXE' (WINDOWS COMMAND PROCESSOR)
REM ----- | |ALSO READ THE NEXT COMMENTS ABOUT SETTING SWITCHES   

    set _MYPROGRAM_=upx.exe

REM ----- ----------------------------------------   
REM ----- |COMMAND-LINE PROGRAM TO BE RUN:  SWITCHES (TO BE PASSED TO THE PROGRAM)
REM ----- |
REM ----- | |YOU CAN LEAVE THIS VARIABLE WITH A BLANK VALUE IF RUNNING THE PROG WITHOUT ANY PARAMETERS
REM ----- | | 
REM ----- | | |IF THE VALUE CONTAINS THE CHARS  '|'  OR  '<'  OR  '>'  THIS BATCH WILL FAIL TO RUN !!!
REM ----- | | |
REM ----- | | | |YOU CAN APPEND EXTRA TEXT TO THE END OF THE COMMAND BY USING THE AMPERSAND CHARACTER '&'
REM ----- | | | | EXAMPLES:
REM ----- | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4 & this text will be ignored; useful for adding some inline comments.
REM ----- | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4         & this works too, and the extra blank spaces will also be ignored.
REM ----- | | | | 
REM ----- | | | | |IF PROG TO BE RUN IS 'CMD.EXE' (WINDOWS COMMAND PROCESSOR), PATH ARGUMENTS MUST POINT TO A SPECIFIC LOCATION
REM ----- | | | | |AND BE ABSOLUTE OR INCLUDE ENVIRONMENT VARIABLES, AND ALWAYS ENCLOSE PATHS IN QUOTES.
REM ----- | | | | | EXAMPLES:
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "C:\Folder With Spaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "C:\FolderWithoutSpaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%SYSTEMROOT%\..\Folder With Spaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%SYSTEMROOT%\..\FolderWithoutSpaces\File.txt"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type %0                                       <<<<<<<this batch file (quotes are optional; the only exception to the rule)
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%~dp0%\..\File.txt"                     <<<<<<<goes back one Dir from this batch file
REM ----- | | | | | set _MYPROGRAMSWITCHES_=type "%~dp0%\..\SomeFolder\File.txt"          <<<<<<<goes back one Dir from this batch file and then forwards into "SomeFolder"
REM ----- | | | | | set _MYPROGRAMSWITCHES_=dir "%systemroot%\system32" /a/o/s/4          <<<<<<<displays directory listing of the System folder. NOTE: 'dir' by itself and
REM ----- | | | | |                                                                                without a specific path argument will resolve to the folder of this batch file.

    set _MYPROGRAMSWITCHES_=--best -v -o getpids-compressed.exe getpids.exe      &// original is 280 KB, will compress down to 88 KB

REM ----- ----------------------------------------
REM ----- |COMMAND-LINE PROGRAM TO BE RUN:  DIRECTORY PATH (OF THE PROGRAM)
REM ----- | 
REM ----- | |YOU CAN LEAVE THIS VARIABLE WITH A BLANK VALUE IF THE PROG IS AT SAME LOCATION AS BATCH
REM ----- | |THIS VARIABLE WILL BE IGNORED IF THE PROG IS 'CMD.EXE'
REM ----- | | 
REM ----- | | EXAMPLES:
REM ----- | | set _MYPROGRAMDIR_=.\                               <<<<<<<relative path, program and batch in same directory
REM ----- | |                                                                  dot backslash *OR* dot (trailing backslash is optional).
REM ----- | | set _MYPROGRAMDIR_=%dp0%                            <<<<<<<relative path, program and batch in same directory
REM ----- | |                                                                  same locatiion as previous but using environment variable.
REM ----- | | set _MYPROGRAMDIR_=..\                              <<<<<<<relative path, program is back one directory from batch
REM ----- | |                                                                  dot dot backslash *OR* dot dot (trailing backslash is optional).
REM ----- | | set _MYPROGRAMDIR_=%dp0%\..                         <<<<<<<relative path, program is back one directory from batch
REM ----- | |                                                                  same as location as previous but using environment variable.
REM ----- | | set _MYPROGRAMDIR_=%SYSTEMROOT%\..\My Utilities     <<<<<<<relative path, program is back one directory from the Windows folder
REM ----- | |                                                                  and then forwards into the folder 'My Utilities'
REM ----- | | set _MYPROGRAMDIR_=C:\My Utilities\                 <<<<<<<absolute path, enclosing quotes are optional; trailing backslash is optional
REM ----- | |                                                                  same location as previous

    set _MYPROGRAMDIR_=

REM ----- ----------------------------------------
REM ----- |SET THE WANTED DIMENSIONS OF THIS CONSOLE WINDOW

    set /a _WINW_=(%_SCREENW_% / 2) + 250
    set /a _WINH_=(%_SCREENH_% / 2) + 150
    set /a _WINX_=(%_SCREENW_% - %_WINW_%) / 2
    set /a _WINY_=(%_SCREENH_% - %_WINH_%) / 2

REM ----- ----------------------------------------
REM ----- |STYLE THIS CONSOLE WINDOW   !!!IMPORTANT!!!
REM ----- |
REM ----- | |IF WE DO NOT USE THE 'MODE' COMMAND HERE ALONG WITH ITS 'COLS/LINES' VALUES THEN
REM ----- | |THE POSITIONING/SIZING OF THIS WINDOW BY 'NIRCMD.EXE' FURTHER DOWN WILL NOT WORK !!!
REM ----- | |
REM ----- | | |SOME COMMANDS WILL OUTPUT A LOT OF LINES AND AND THE CONSOLE DISPLAY WILL BE TRUNCATED
REM ----- | | |RUN THIS FOR AN EXAMPLE=====>   cmd.exe dir "%systemroot%\system32" /a/o/s/4   <=====
REM ----- | | |TO SOLVE THIS PROBLEM, SET THE 'LINES' VALUE OF 'MODE' TO A VERY HIGH NUMBER
REM ----- | | |THE MAXIMUM NUMBER IS 32000 (ON MY SYSTEM); 'MODE' WILL BE IGNORED IF THE NUMBER IS HIGHER !!!   

    color 0A
    mode.com con:cols=%_SCREENW_% lines=10000

REM ----- ----------------------------------------
REM ----- |DELETE THE TEMP TEXT FILE MADE BY 'MonitorInfoView.exe'
REM ----- |COMMENT OUT THE LINE IF YOU DON'T CARE ABOUT DELETING IT

    del /f /q ~TMP.TXT 2>nul >nul

REM >>>>>>>>>> USER INPUT/PREFERENCES ARE ALL SET HERE [END] <<<<<<<<<<<<<<
REM >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<


REM ----- RUN THE TASK . . .
nircmd.exe win setsize foreground %_WINX_% %_WINY_% %_WINW_% %_WINH_%
popd

if not defined _MYPROGRAMDIR_ set _MYPROGRAMDIR_=%~dp0
if "!_MYPROGRAMDIR_!" == "." set _MYPROGRAMDIR_=%~dp0
if "!_MYPROGRAMDIR_!" == ".\" set _MYPROGRAMDIR_=%~dp0
pushd %_MYPROGRAMDIR_%
if not exist %_MYPROGRAMDIR_% goto _SKIP_
set _CONVERT_PATH_="%CD%"
set _CONVERT_PATH_=%_CONVERT_PATH_:"=%
set _MYPROGRAMDIR_=%_CONVERT_PATH_%
:_SKIP_
popd

echo ===============================================================================
echo PROGRAM ..............: !_MYPROGRAM_!
if "!_MYPROGRAM_!"=="cmd.exe" (echo PROGRAM DIR ..........: %SYSTEMROOT%\system32) else (echo PROGRAM DIR ..........: !_MYPROGRAMDIR_!)
echo PROGRAM SWITCHES .....: !_MYPROGRAMSWITCHES_!
echo ===============================================================================

if "!_MYPROGRAM_!"=="cmd.exe" (goto _PROGRAM_IS_CMD_) else (goto _PROGRAM_IS_OTHER_)

:_PROGRAM_IS_CMD_
cd /d "%SYSTEMROOT%\system32"
!_MYPROGRAMSWITCHES_!
echo. & echo. & echo.
goto _END_

:_PROGRAM_IS_OTHER_ 
cd /d "!_MYPROGRAMDIR_!"
if "%CD%\!_MYPROGRAM_!" == "%CD%\" goto _ERRORMESSAGE_
if not exist "%CD%\!_MYPROGRAM_!" goto _ERRORMESSAGE_
"!_MYPROGRAMDIR_!\!_MYPROGRAM_!" !_MYPROGRAMSWITCHES_!
echo. & echo. & echo.
goto _END_

:_ERRORMESSAGE_
cls
color 4F
echo ===============================================================================
echo PROGRAM ..............: !_MYPROGRAM_!
echo PROGRAM DIR ..........: !_MYPROGRAMDIR_!
echo PROGRAM SWITCHES .....: !_MYPROGRAMSWITCHES_!
echo ===============================================================================
echo                        ^^!^^!^^!  E R R O R  ^^!^^!^^!
echo.
echo The program has failed to run; the path set by the above values does not exist.
echo Please exit this window and check the values you have set in this batch file.
echo.
echo This batch file is located here:
echo %0
echo. & echo. & echo. & echo. & echo.

:_END_

REM ----- CLEAR VARIABLE VALUES FROM MEMORY . . .
set _PROCESS_ID_=
set _SCREENW_=
set _SCREENH_=
set _MYPROGRAM_=
set _MYPROGRAMSWITCHES_=
set _MYPROGRAMDIR_=
set _WINW_=
set _WINH_=
set _WINX_=
set _WINY_=
set _CONVERT_PATH_=

REM ----- STOP THIS CONSOLE WINDOW FROM CLOSING   !!!IMPORTANT!!! . . .
REM ----- |
REM ----- | http://superuser.com/questions/306167/how-to-prevent-the-command-prompt-from-closing-after-execution
REM ----- | | by 'kitsu.eb' (April 26, 2014)
REM ----- | | | "My solution (just tested on Win7) is to add CMD as the last line in the batch file.
REM ----- | | |  This runs a nested command prompt that inherits the environment of its parent.
REM ----- | | |  That child shell holds the batch process open until you EXIT, at which point the
REM ----- | | |  batch has no child processes and also exits."
REM ----- | Comment . . .
REM ----- | | by me
REM ----- | | | "This is a workaround. You can't use PAUSE as the last command to stop this window from closing
REM ----- | | |  because the window will terminate after you press a key. While this batch is running, if you open
REM ----- | | |  Task Manager/Process Hacker/Process Explorer you will see an extra CMD.EXE child process running
REM ----- | | |  under the parent CMD.EXE of this batch file."
cmd.exe
1
  • It'd be perfectly fine to copy the relevant contents here, too.
    – slhck
    Aug 23, 2014 at 8:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .