10

I've recently learned that I can control the size of the CMD window running my program with mode x,y. However I just as recently noticed that this sets the buffer size, and the window will adjust to match or max out at the screen size.

I would like to use mode 100,50 for the window size, but I also want to keep a arger buffer - for development at least I want mode 100,9999.

Is there any way to do this?

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592

6 Answers6

27

I don't think there is a native batch command that gives independent control of buffer and window sizes. .NET can control both, but I don't think VBScript or JScript can access that functionality. But powershell can :-) See How Can I Expand the Width of the Windows PowerShell Console?

Thankfully, the new settings are preserved in the CMD window when PowerShell exits.

It is important that the window size is always less than or equal to the buffer size. To simplify things, I first use MODE to set both the window and buffer to the desired window size, and then I use powershell to set the buffer size.

Here is a simple conSize.bat hybrid batch/powershell script that takes the sizes as parameters:

@echo off
:conSize  winWidth  winHeight  bufWidth  bufHeight
mode con: cols=%1 lines=%2
powershell -command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%3;$B.height=%4;$W.buffersize=$B;}"

To get your desired size, you would simply use

call conSize 100 50 100 9999
dbenham
  • 127,446
  • 28
  • 251
  • 390
2

There's a 'ConSetBuffer' binary available that does specifically this, and I've found it to work reliably. It and related console utilities are available at the 'conutils.zip' link on this page.

user117529
  • 663
  • 8
  • 16
  • Useful tool, although its unfortunate the same package doesn't include a tool to modify window screen size (independently of window buffer size) as well. – dwillis77 Jan 12 '21 at 21:53
1

I have written a tiny application for Windows that allows to "maximize" the window and buffer. It could be easily extended to allow passing parameters with custom values.

Antoni Sawicki
  • 166
  • 1
  • 7
0

here another variant:

/*
@echo off & mode 100,50
set "cscfile="
set Pathfile="%WinDir%\Microsoft.NET\Framework\csc.exe"
for /f "delims=" %%a in ('dir /b /a-d /s %PathFile%') do set "cscfile=%%a"
if defined cscfile (
  %cscfile% /nologo /out:"%~0.exe" %0
) else exit /b
"%~0.exe"
del "%~0.exe" 
cmd /k
*/

using System;

class Program
  {
     static void Main()
     { 
        Console.SetBufferSize(100, 9999); 
        Console.ForegroundColor = ConsoleColor.Green;
        Console.WriteLine("Current Logged UserName: " + Environment.UserName);
        Console.ResetColor();
        Console.WriteLine("[Enter] to continue");
        Console.ReadLine();
     }
  }
walid2mi
  • 2,704
  • 15
  • 15
0

The answer to this question didn't work as I got an error regarding variables sent to the PowerShell command, so I modified to work.

The PowerShell command simply adds uiHeightBuffer to the uiHeight variable to show the scroll bar.


Edit: Added the height buffer as the 3rd parameter and TRUE or FALSE as a 4th parameter to enable or disable the scrollbar.

I found this useful when exiting a script with a small message when there's no need for the scrollbar in this case.

Also to enable UTF-8 encoding you have to set the default at the start of the `:CMDSIZE' call and set it at the end, otherwise, the font size goes a bit funny.

Omitting chcp 850 >NUL & and chcp 65001 >NUL & are what you'd do if you didn't want to affect this.

This works in Version: 1909 of Windows 10.

@echo off
title %~nx0
rem Based on this thread here: https://stackoverflow.com/a/13351373
goto MYROUTINE
:CMDSIZE
chcp 850 >NUL & set "uiWidth=%1" & set "uiHeight=%2"
mode %uiWidth%,%uiHeight%
if %4==TRUE (set /a "uiHeightBuffer=uiHeight+%3")
if %4==TRUE (powershell.exe -ExecutionPolicy Bypass -Command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeightBuffer%;$W.buffersize=$B}")
if %4==FALSE (powershell.exe -ExecutionPolicy Bypass -Command "&{$H=get-host;$W=$H.ui.rawui;$B=$W.buffersize;$B.width=%uiWidth%;$B.height=%uiHeight%;$W.buffersize=$B}")
chcp 65001 >NUL & goto :EOF
:MYROUTINE
call :CMDSIZE 255 44 222 TRUE
title Do your routine here...
echo Do your routine here...
echo/ & pause
goto :EXITROUTINE
:EXITROUTINE
call :CMDSIZE 40 2 0 FALSE
title Exiting routine message...
echo Exiting routine message...
set /p "=" <NUL
ping localhost -n 3 >NUL & exit
Ste
  • 1,729
  • 1
  • 17
  • 27
-1

I have found a way to resize the buffer size without influencing the window size. It works thanks to a flaw in how batch works but it gets the job done.

mode 648 78 >nul 2>nul

How does it work? There is a syntax error in this command, it should be "mode 648, 78". Because of how batch works, the buffer size will first be resized to 648 and then the window resize will come but it will never finish, because of the syntax error. Voila, buffer size is adjusted and the window size stays the same. This produces an ugly error so to get rid of it just add the ">nul 2>nul" and you're done.

Myzreal
  • 351
  • 4
  • 16
  • First parameter is the horizontal buffer, though, so even if this worked (it doesn't...) it would only give horizontal resize, wouldn't it? – Niet the Dark Absol May 15 '14 at 10:50
  • Doesn't work at all for me on Win 7. It just gives the error message, and no change to buffer size (neither horizontal nor vertical) – dbenham May 15 '14 at 12:56