commands to change CmdPrompt buffer size?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 108
Joined: 09 Nov 2011 01:42

commands to change CmdPrompt buffer size?

#1 Post by pstein » 11 Aug 2017 03:37

When I enter in a CmdPrompt under Win7 a command like

mode con cols=80 lines=50

then the number of visible rows and columns are changed accordingly.

This command seems not to work any longer in Win10.
Is this true?

Furthermore I am searching for a similar way to change (=increase) the current line/history and column buffer (!) size
for the current CmdPrompt window.

Is there such a command or a parameter for DOS batch scripts?

In which registry key are the two buffer sizes stored in Win7 and Win10?

Thank you
Peter

elzooilogico
Posts: 113
Joined: 23 May 2016 15:39
Location: Spain

Re: commands to change CmdPrompt buffer size?

#2 Post by elzooilogico » 11 Aug 2017 04:39

Don't know nothing about Win 10, but here (Win8) under
      HKEY_CURRENT_USER\Console key, you may find
        ScreenBufferSize 0x012c0050
        WindowSize 0x00190050

Both are DWORD values, where hiword is lines and loword is columns.

So ScreenBufferSize.Lines is 0x012c (decimal 300) and ScreenBufferSize.Columns is 0x0050 (dec. 80), WindowSize.Lines is 0x0019 (dec. 25) WindowSize.Columns is 0x0050 (dec. 80)

You may use powershell to change window and buffer size.

Edit 17/08/2017 corrected typos.

Code: Select all

@echo off
SetLocal EnableExtensions EnableDelayedExpansion

::Get registry values
for /F "skip=1 tokens=2,*" %%A in ('reg query "HKCU\Console" /V "WindowSize"') do set "WinSize=%%B"
for /F "skip=1 tokens=2,*" %%A in ('reg query "HKCU\Console" /V "ScreenBufferSize"') do set "BufSize=%%B"

set "winCols=%WinSize:~-4%"
set "winLines=!WinSize:%winCols%=!" & set "winCols=0x!winCols!"
set "bufCols=%BufSize:~-4%"
set "bufLines=!BufSize:%bufCols%=!" & set "bufCols=0x!bufCols!"

::convert to Decimal
set/a winCols=winCols, winLines=winLines, bufCols=bufCols, bufLines=bufLines
echo Current sizes (Cols x Lines)
echo ----------------------------
echo Window size:  %winCols% %winLines%
echo Buffer size:  %bufCols% %bufLines%

:: Resize the console window and buffer
call :setWin 80 25 80 300

:: your script here.
:: blah blah
:: blah blah

EndLocal
exit/B

:setWin <columns> <lines> <buffercolumns> <bufferlines>
SetLocal
set "psCmd=powershell -Command "$myhost=get-host;$win=$myhost.ui.rawui;"
set "psCmd=%pscmd%$size=$win.windowsize;$size.width=%1;$size.height=%2;$win.windowsize=$size;"
set "psCmd=%pscmd%$size=$win.buffersize;$size.width=%3;$size.height=%4;$win.buffersize=$size;""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "result=%%#"
EndLocal
exit/B
Last edited by elzooilogico on 17 Aug 2017 06:01, edited 1 time in total.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: commands to change CmdPrompt buffer size?

#3 Post by thefeduke » 11 Aug 2017 17:27

I have had good luck using:

Code: Select all

        Echo.Refer to Post subject: ^"Cmdwiz - cmd helper tools^" by misol101
        Echo.at http://www.dostips.com/forum/viewtopic.php?p=48971#p48971
        Echo.for download instructions.
Here is an example of use:

Code: Select all

    If "%BufSw%" EQU "1" CmdWiz SetBufferSize %ScrBuffColS% %ScrBuffRows%

John A.

ShadowThief
Expert
Posts: 908
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: commands to change CmdPrompt buffer size?

#4 Post by ShadowThief » 11 Aug 2017 19:03

It works fine; who said it doesn't?

Image

npocmaka_
Posts: 481
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: commands to change CmdPrompt buffer size?

#5 Post by npocmaka_ » 11 Aug 2017 19:53

it can be easy done with embedded .net code (you can choose among c#,jscript.net,vb,powershell) -> https://msdn.microsoft.com/en-us/librar ... e(v=vs.110).aspx

pstein
Posts: 108
Joined: 09 Nov 2011 01:42

Re: commands to change CmdPrompt buffer size?

#6 Post by pstein » 12 Aug 2017 23:28

elzooilogico wrote:Don't know nothing about Win 10, but here (Win8) under
      HKEY_CURRENT_USER\Console key, you may find
        ScreenBufferSize 0x012c0050
        WindowSize 0x00190050

Both are DWORD values, where hiword is lines and loword is columns.

So ScreenBufferSize.Lines is 0x012c (decimal 300) and ScreenBufferSize.Columns is 0x0050 (dec. 80), WindowSize.Lines is 0x0019 (dec. 25) WindowSize.Columns is 0x0050 (dec. 80)


This is interesting.
I investigated the Registry of my current system Windows 7 64bit.
I have exactly the values you mentioned.
But when I go visually into Menu "Properties--->Layout tab of my CmdPrompt window then there are the following values (They correspond to what i can see on the desktop):
Screen Buffer size Width=300
Screen Buffer Size Height=9999
Windows Size Width=150
Windows Size Height=60

As you can see here is a big difference in all 4 values.

So your suggested registry values seems only to be the default values.
Or they are only responsible for 32bit systems
or for the whole machine instead of the current user
...or whatever

So how do I find out where the Registry Values for my real CmdPrompt values are stored?

Thank you

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: commands to change CmdPrompt buffer size?

#7 Post by thefeduke » 13 Aug 2017 00:29

pstein wrote:So your suggested registry values seems only to be the default values.
Correct choice of words.

If you right-click the prompt title-bar you can choose Defaults or Properties.

Defaults should match the registry.

Properties should match current prompt and will change as you re-dimension the window. They should match the properties in the starting shortcut before changes, and match the registry if opened by a start command.
'MODE con' shows buffer values. 'Mode con cols=400' will attempt to widen the screen to the new buffer value but is constrained by screen size and current properties will show 400 for buffer width and a smaller number for screen width.

You cannot drag a prompt window larger than the buffer size or physical screen size, but if the window is narrowed by mouse, the output of 'MODE con' commend can be interpreted, but won't show the shrinkage.

John A.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: commands to change CmdPrompt buffer size?

#8 Post by thefeduke » 16 Aug 2017 00:34

Apologies to pstein if I am straying here.
elzooilogico wrote:You may use powershell to change window and buffer size

Code: Select all

:setWin <columns> <lines> <buffercolumns> <bufferlines>
SetLocal
set "psCmd=powershell -Command "$host=get-host;$win=$host.ui.rawui;"
set "psCmd=%pscmd%$size=$win.windowsize;$size.width=%1;$size.height=%2;$win.windowsize=$size;"
set "psCmd=%pscmd%$size=$win.buffersize;$size.width=%3;$size.height=%4;$win.buffersize=$size;""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "result=%%#"
EndLocal
exit/B
Very impressive. I had to explore. I took your DOS powershell command out of context to try to understand this magic. your FOR construct seems to run %psCmd% in a way that hides the output. That consists of error messages about not being able to update the system variable $host. Yet the routine works. Fool that I am, I changed $host to $myhost and the errors disappeared and it still worked. More exploring. Screen print:

Code: Select all

type Cmdps.bat
powershell -Command "echo :;$myhost=get-host;echo myhost_variable_is:$myhost $myhost;$wi
n=$myhost.ui.rawui;echo win_variable_is:$win $win;echo :;$size=$win.windowsize;echo size
_var=$size $size;$size.width=65;echo size_var=$size $size;$size.height=43;echo size_var=
$size $size;$win.windowsize=$size;echo win_variable_is:$win $win;echo :;"
This showed me a lot. Now I understand what I really don't understand:

Code: Select all

:
myhost_variable_is:System.Management.Automation.Internal.Host.InternalHost


Name             : ConsoleHost
Version          : 2.0
InstanceId       : 31998504-2e21-4d47-9023-be4c7566a9d5
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

win_variable_is:System.Management.Automation.Internal.Host.InternalHostRawUserInterface
ForegroundColor       : Gray
BackgroundColor       : Black
CursorPosition        : 0,221
WindowPosition        : 0,178
CursorSize            : 25
BufferSize            : 88,222
WindowSize            : 88,44
MaxWindowSize         : 88,48
MaxPhysicalWindowSize : 145,48
KeyAvailable          : False
WindowTitle           : C:\Windows\system32\cmd.exe - cmdps

:
size_var=88,44
Width  : 88
Height : 44

size_var=65,44
Width  : 65
Height : 44

size_var=65,43
Width  : 65
Height : 43

win_variable_is:System.Management.Automation.Internal.Host.InternalHostRawUserInterface
ForegroundColor       : Gray
BackgroundColor       : Black
CursorPosition        : 0,221
WindowPosition        : 0,179
CursorSize            : 25
BufferSize            : 88,222
WindowSize            : 65,43
MaxWindowSize         : 88,48
MaxPhysicalWindowSize : 145,48
KeyAvailable          : False
WindowTitle           : C:\Windows\system32\cmd.exe - cmdps

:
Where would a code-cloner, like me, find about these magic powershell system variables? I Googled myself silly and kept running into DOS environmental variables in powershell. There must be a table somewhere.

I am looking for a system variable that might be applied to another active subject: Re: How to open a CMD Window at a specified location on the Desktop? http://www.dostips.com/forum/viewtopic.php?p=53361#p53361
John A.

elzooilogico
Posts: 113
Joined: 23 May 2016 15:39
Location: Spain

Re: commands to change CmdPrompt buffer size?

#9 Post by elzooilogico » 16 Aug 2017 04:29

thefeduke wrote:Very impressive. I had to explore. I took your DOS powershell command out of context to try to understand this magic. your FOR construct seems to run %psCmd% in a way that hides the output. That consists of error messages about not being able to update the system variable $host. Yet the routine works. Fool that I am, I changed $host to $myhost and the errors disappeared and it still worked. More exploring. Screen print:


:twisted: Arghh. My fault! My apologies to place a post containing typos. :oops:

(not tested) Here, you'll find a powershell script to resize and move a window https://gallery.technet.microsoft.com/scriptcenter/Set-the-position-and-size-54853527

Surely is not what you pretend, but it may be all done with an hybrid script.

Once the c# exe is build, you can simply call it, and remove the creation stuff , or delete and recreate each time.

NOTE .Net framework must be installed on your system.

Hope it helps.

EDIT 17/08/2017, restore window size when changing buffer size (and little polish).

Code: Select all

//>nul 2>nul||@goto :batch_code
/*
:batch_code
@echo off
SetLocal
rem place desired exe name
set "theExeFile=mySetWinPos.exe"

if not exist "%theExeFile%" call :build_the_exe || exit/B

%theExeFile% w:80,25 b:80,250 p:100,50
:: your script here.
:: blah blah
:: blah blah

rem del "%theExeFile%" >NUL 2>NUL

EndLocal
EndLocal
exit /b 0

:build_the_exe
for /f "tokens=* delims=" %%v in ('dir /b /s /a:-d /o:-n "%SystemRoot%\Microsoft.NET\Framework\csc.exe"') do (
   set "csc=%%v"
)
if "%csc%" == "" echo/&echo/Warning: Net Framework Not Found&exit/B 1
call "%csc%" /nologo /out:"%theExeFile%" "%~dpsfnx0"
exit/B 0
*/


//begin c# code
using System;
using System.Runtime.InteropServices;

/*
op-code may be
  b - to set buffer size
  p - to set window position
  w - to set window size

args may follow the format, op-code:x-axis,y-axis
i,e to change window size and buffer size

%theExeFile% w:80,40 b:80,250
*/

namespace ElZooilogico
{
  class windowPos
  {
    public static uint SWP_NOSIZE = 1;
    public static uint SWP_NOZORDER = 4;

    [DllImport("kernel32.dll", ExactSpelling = true)]
    private static extern IntPtr GetConsoleWindow();
    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, uint wFlags);

    private static IntPtr myconsole = GetConsoleWindow();

    public static int Main(string[] args)
    {
      string[] tmp = null;
      string[] buf = null;

      try {
        if ( args.Length < 1 )
          return 1;

        for ( int i = 0; i < args.Length; i++ )
        {
          tmp = args[i].Split(':');
          if ( tmp.Length == 2 )
          {
            buf = tmp[1].Split(',');

            if ( tmp[0].Equals("w", StringComparison.OrdinalIgnoreCase) )
              Console.SetWindowSize(Int32.Parse(buf[0]), Int32.Parse(buf[1]));
            if ( tmp[0].Equals("p", StringComparison.OrdinalIgnoreCase) )
              SetWindowPos(myconsole, 0, Int32.Parse(buf[0]), Int32.Parse(buf[1]), 0, 0, SWP_NOSIZE | SWP_NOZORDER);
            if ( tmp[0].Equals("b", StringComparison.OrdinalIgnoreCase) )
            {
              int width = Console.WindowWidth, height = Console.WindowHeight;
              // the following two lines are mandatory to decrease buffer size
              Console.Clear();
              Console.SetWindowSize(1, 1);
              Console.SetBufferSize(Int32.Parse(buf[0]), Int32.Parse(buf[1]));
              Console.SetWindowSize(width, height);
            }
          }
        }
      } catch (Exception e) { System.Windows.Forms.MessageBox.Show(e.Message); return 9; }
      //} catch { return 1; }

      return 0;
    }

  } // class windowPos
} // namespace ElZooilogico
Last edited by elzooilogico on 17 Aug 2017 05:09, edited 3 times in total.

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: commands to change CmdPrompt buffer size?

#10 Post by thefeduke » 16 Aug 2017 09:16

thefeduke wrote:Apologies to pstein if I am straying here.
Peter, I rescind my apology. It seems that I asked the right question of the right person in the best circumstances to meet my needs, and more, and hopefully your requirement.
elzooilogico wrote:Hope it helps.
This is more that everything that I asked for. It worked like a charm and I really do appreciate the resource link. I'll get lost there for a long time. I shall name my next-born script file after you. Thanks for a speedy and very valuable reply.

John A

Post Reply