Windows – How to Open a Console Application with a Given Window Size

batch filecommand linewindows

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?

Best Answer

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

Related Question