How to define screen buffer size for "Mode 120,60" command?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

How to define screen buffer size for "Mode 120,60" command?

#1 Post by tobwz » 27 Apr 2022 23:20

When I put the command

mode 120,60

at the top of an batch script then the output command prompt window will have a width of 120 chars and a height 60 rows.
It works.

However sometimes the output of the script is longer than 60 lines.
Therefore I need a way to define a screen buffer size (with scrollbar).

In "normal" Command prompt windows I can go to menu

Properties--->Layout--->Screen Buffer Size--->Height =9000

This works.

When I do the same within a Commandprompt window WITH ""MODE" command this does NOT work.
I can defined a screen buffer size. But the next time I run the script this screen buffer size is reset to 60 (=same as number of visual rows).

So how can I define (in mode command) an additional buffer size?
Or make buffer size from menu permanent?

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: How to define screen buffer size for "Mode 120,60" command?

#2 Post by Aacini » 28 Apr 2022 05:30

I don't remember the exact procedure to make buffer size from menu permanent, but I think was this:
  1. Run your mode command
  2. Run wmic with no parameters. A wmic prompt is open, for example:

    Code: Select all

    wmic:root\cli>
    
  3. At this point, go to menu (right click on cmd window) and complete your Properties--->Layout--->Screen Buffer Size--->Height =9000
  4. Enter "exit" to close wmic.
Please, report the result...

Antonio

atfon
Posts: 178
Joined: 06 Oct 2017 07:33

Re: How to define screen buffer size for "Mode 120,60" command?

#3 Post by atfon » 28 Apr 2022 06:15

If you are open to powershell, I've done this from a batch script:

Code: Select all

powershell.exe -noprofile -command "&{$w=(get-host).ui.rawui;$w.buffersize=@{width=120;height=9001};$w.windowsize=@{width=120;height=60};}"

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to define screen buffer size for "Mode 120,60" command?

#4 Post by tobwz » 28 Apr 2022 22:31

Aacini wrote:
28 Apr 2022 05:30
I don't remember the exact procedure to make buffer size from menu permanent, but I think was this:
  1. Run your mode command
  2. Run wmic with no parameters. A wmic prompt is open, for example:

    Code: Select all

    wmic:root\cli>
    
  3. At this point, go to menu (right click on cmd window) and complete your Properties--->Layout--->Screen Buffer Size--->Height =9000
  4. Enter "exit" to close wmic.
Ok, this works for the CommandPrompt when I open just the Commandprompt.
Therefore it is not necessary to open wmic

But if I exit CommandPrompt and double click afterwards on a *.bat batch script with a "mode 120,60" statement at the top
then previously set buffersize is NOT recognized/setup.
buffersize is windowsize

So this is not a solution

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to define screen buffer size for "Mode 120,60" command?

#5 Post by tobwz » 28 Apr 2022 22:36

atfon wrote:
28 Apr 2022 06:15
If you are open to powershell, I've done this from a batch script:

Code: Select all

powershell.exe -noprofile -command "&{$w=(get-host).ui.rawui;$w.buffersize=@{width=120;height=500};$w.windowsize=@{width=120;height=60};}"

When I replace the "mode 120,60" command at the top of a batch script by your powershell command it works - partially.
A red error notification is shown:
Exception setting "buffersize": "Cannot set the buffer size because the size specified is too large or too small.
Parameter name: value
Actual value was 120,500."
At line:1 char:26
+ ... {$w=(get-host).ui.rawui;$w.buffersize=@{width=120;height=500};$w.wind ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
+ FullyQualifiedErrorId : ExceptionWhenSetting
Though the buffer seems to be setup anyway.

But the ugly red error text is misleading and should vanish.
How can I get rid of it?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to define screen buffer size for "Mode 120,60" command?

#6 Post by penpen » 29 Apr 2022 02:43

You should test, whether or not the error text really is misleading. Since the buffer is set first, it might be smaller than the actual window size at that point:

Code: Select all

powershell.exe -noprofile -command "&{Write-Host "initial WindowSize $(get-host).UI.RawUI.WindowSize"; $w=(get-host).ui.rawui;$w.buffersize=@{width=120;height=500};$w.windowsize=@{width=120;height=60};}"

penpen

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to define screen buffer size for "Mode 120,60" command?

#7 Post by aGerman » 29 Apr 2022 11:23

I think penpen is right. You should rather apply kinda clamped math here. That means, determine the maximum window size first, compare with your settings, and take care you don't exceed the maximum values.
Something about like that:

Code: Select all

@echo off &setlocal

:::::::::::::::::::
setlocal DisableDelayedExpansion
set set_clamped_console_window_size=for %%- in (1 2) do if %%-==2 (^
%=% setlocal EnableDelayedExpansion^&for /f "tokens=1,2" %%. in ("^^!p^^!") do^
%===% endlocal^&powershell.exe -nop -ep Bypass -c ^"$c=Add-Type -Name pInv -PassThru -MemberDefinition '^
%=====% [DllImport(\"shcore.dll\")] public static extern void SetProcessDpiAwareness(int val);^
%=====% [DllImport(\"user32.dll\")] public static extern void SetProcessDPIAware();^
%=====% [DllImport(\"user32.dll\")] public static extern int GetSystemMetrics(int idx);^
%=====% [DllImport(\"kernel32.dll\")] public static extern IntPtr CreateFile(string name,int acc,int share,IntPtr sec,int how,int flags,IntPtr tmplt);^
%=====% [DllImport(\"kernel32.dll\")] public static extern void GetCurrentConsoleFont(IntPtr hOut,int maxWnd,short[] info);^
%=====% [DllImport(\"kernel32.dll\")] public static extern void CloseHandle(IntPtr handle);';^
%===% try{$c::SetProcessDpiAwareness(2)}catch{$c::SetProcessDPIAware()}^
%===% $hOut=$c::CreateFile('CONOUT$',-1073741824,2,[IntPtr]::Zero,3,0,[IntPtr]::Zero);^
%===% [Int16[]]$fInf=0,0,0,0;$c::GetCurrentConsoleFont($hOut,0,$fInf);$c::CloseHandle($hOut);^
%===% [int]$xmin=$c::GetSystemMetrics(28)/$fInf[2]+1;^
%===% $w=$host.UI.RawUI;$xmax=$w.MaxPhysicalWindowSize.Width;$ymax=$w.MaxPhysicalWindowSize.Height;^
%===% $x=[Math]::Min($xmax,[Math]::Max($xmin,%%~.));$y=[Math]::Min($ymax,[Math]::Max(1,%%~/));^
%===% try{$w.BufferSize=New-Object Management.Automation.Host.Size($x,9001)}catch{}^
%===% $w.WindowSize=New-Object Management.Automation.Host.Size($x,$y);^
%===% $w.BufferSize=New-Object Management.Automation.Host.Size($x,9001);^"^
%=% ^&endlocal) else setlocal DisableDelayedExpansion^&set p=
endlocal&set "set_clamped_console_window_size=%set_clamped_console_window_size%"
if !!# neq # set "set_clamped_console_window_size=%set_clamped_console_window_size:^^!=!%"
:::::::::::::::::::

%set_clamped_console_window_size% 20 1000
pause
%set_clamped_console_window_size% 1000 20
pause
%set_clamped_console_window_size% -1000 -1000
pause
%set_clamped_console_window_size% 1000 1000
pause
This code is also backward compatible to PowerShell 2 btw.

Steffen
Last edited by aGerman on 06 Jun 2022 08:25, edited 5 times in total.
Reason: shortened

tobwz
Posts: 30
Joined: 25 Feb 2022 03:21

Re: How to define screen buffer size for "Mode 120,60" command?

#8 Post by tobwz » 29 Apr 2022 13:11

penpen wrote:
29 Apr 2022 02:43
You should test, whether or not the error text really is misleading. Since the buffer is set first, it might be smaller than the actual window size at that point:

Code: Select all

powershell.exe -noprofile -command "&{Write-Host "initial WindowSize $(get-host).UI.RawUI.WindowSize"; $w=(get-host).ui.rawui;$w.buffersize=@{width=120;height=500};$w.windowsize=@{width=120;height=60};}"
@penpen:

This code looks good.

@aGerman.

I will check your advanced code

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to define screen buffer size for "Mode 120,60" command?

#9 Post by penpen » 30 Apr 2022 05:02

I only added a debug output, so you can see whether or not the error message is misleading.
I didn't change the program logic.

Post Reply