How can I open a console application with a given window size?

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?


Solution 1:

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

Solution 2:

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

Solution 3:

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.