How to define screen buffer size for "Mode 120,60" command?
Moderator: DosItHelp
How to define screen buffer size for "Mode 120,60" command?
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?
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?
Re: How to define screen buffer size for "Mode 120,60" command?
I don't remember the exact procedure to make buffer size from menu permanent, but I think was this:
Antonio
- Run your mode command
- Run wmic with no parameters. A wmic prompt is open, for example:
Code: Select all
wmic:root\cli>
- At this point, go to menu (right click on cmd window) and complete your Properties--->Layout--->Screen Buffer Size--->Height =9000
- Enter "exit" to close wmic.
Antonio
Re: How to define screen buffer size for "Mode 120,60" command?
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};}"
Re: How to define screen buffer size for "Mode 120,60" command?
Ok, this works for the CommandPrompt when I open just the Commandprompt.Aacini wrote: ↑28 Apr 2022 05:30I don't remember the exact procedure to make buffer size from menu permanent, but I think was this:
- Run your mode command
- Run wmic with no parameters. A wmic prompt is open, for example:
Code: Select all
wmic:root\cli>
- At this point, go to menu (right click on cmd window) and complete your Properties--->Layout--->Screen Buffer Size--->Height =9000
- Enter "exit" to close wmic.
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
Re: How to define screen buffer size for "Mode 120,60" command?
atfon wrote: ↑28 Apr 2022 06:15If 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:
Though the buffer seems to be setup anyway.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
But the ugly red error text is misleading and should vanish.
How can I get rid of it?
Re: How to define screen buffer size for "Mode 120,60" command?
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:
penpen
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
Re: How to define screen buffer size for "Mode 120,60" command?
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:
This code is also backward compatible to PowerShell 2 btw.
Steffen
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
Steffen
Last edited by aGerman on 06 Jun 2022 08:25, edited 5 times in total.
Reason: shortened
Reason: shortened
Re: How to define screen buffer size for "Mode 120,60" command?
@penpen:penpen wrote: ↑29 Apr 2022 02:43You 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};}"
This code looks good.
@aGerman.
I will check your advanced code
Re: How to define screen buffer size for "Mode 120,60" command?
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.
I didn't change the program logic.