Reputation: 31
I would like to make a *.bat file which can act differently according to the window size of the CMD.
I searched for a way to find the window size which is mostly the same but some times when people customize the settings its not the same. Normally the window size is 80 characters width and 25 lines height. But some apply their own custom settings so it could be any thing else.
Is there a way from inside of a *.bat to find the actual window size? I would like to keep it as much *.bat as possible ... with other words not PowerShell or vbs and so on and the best would also be without *.tmp files.
This would interest me the most. But also interesting would be a way to find the buffer size of a running *.bat in it self.
Is there any way or idea to get this information from inside of a *bat?
Alternative may would be to adjust the window size or buffer size from a *.bat file it self. So if I can't get the actual window or buffer size is it possible to set a window or buffer size in a running *.bat while it is processed?
Does any one have a trick or even idea on how to get or set this information from inside of a running *.bat?
Until now I have no idea if it is possible and how? so I could not try much that I could show now.
UPDATE:
REM Empty Variables
set "ROWS="
set "CHARACTERS="
REM Read CMD Window Size in Rows and Characters per Row.
for /F "tokens=3" %%A in ('reg query HKCU\Console /v WindowSize') do set /A ROWS=%%A/65535
for /F "tokens=3" %%A in ('reg query HKCU\Console /v WindowSize') do set /A "CHARACTERS=%%A&0xffff
REM Test it ...
echo %ROWS%
echo %CHARACTERS%
This will read the rows and characters per line from the registry. its a shortened form from https://superuser.com/questions/1330772/cmd-window-size
Upvotes: 0
Views: 1586
Reputation: 38719
Just for the sake of it, a batch-file or cmd could leverage powershell for this:
"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "$HW=(Get-Host).UI.RawUI;'Window size = '+$HW.WindowSize;'Buffer size = '+$HW.BufferSize"
This should provide an output similar to the following, (where the first number is the Width and the second the Height):
Window size = 80,30
Buffer size = 80,120
Obviously you could adjust the output to your specific requirements, e.g.
"%__AppDir__%WindowsPowerShell\v1.0\powershell.exe" -NoP "$HW=(Get-Host).UI.RawUI;'WinWxH='+$HW.WindowSize -Replace ',','x';'BufWxH='+$HW.BufferSize -Replace ',','x'"
Example output:
WinWxH=80x30
BufWxH=80x120
That would mean if you wanted those values as variables, in your batch-file, you could run the command within a for-loop and save the output with Set
:
@For /F Delims^= %%G In ('PowerShell -NoP "$HW=(Get-Host).UI.RawUI;'WinWxH='+$HW.WindowSize -Replace ',','x';'BufWxH='+$HW.BufferSize -Replace ',','x'"')Do @Set "%%G"
Your variables in this case would be %WinWxH%
with a defined value of 80x30
, and %BufWxH%
with a defined value of 80x120
.
Upvotes: 0
Reputation: 34989
You can use mode con
to get the buffer size in terms of characters:
for /F "tokens=1* delims=: " %%I in ('
mode con ^| findstr "Lines Columns"
') do set "%%I=%%J"
echo Lines=%Lines%
echo Columns=%Columns%
Note that such mode con
approaches are locale-dependent!
To avoid that, you could use this:
rem // Ensure variables are empty:
set "Lines=" & set "Columns="
rem /* Skip over header lines and use `:` as the only delimiter, because
rem keywords may contain spaces on their own in other languages: */
for /F "skip=2 tokens=1* delims=:" %%I in ('mode con') do (
rem // Remove surrounding spaces from extracted value:
for /F %%K in ("%%J") do (
if not defined Lines (
rem // First value after header line is supposed to be lines:
set "Lines=%%K"
) else if not defined Columns (
rem // Second value after header line is supposed to be columns:
set "Columns=%%K"
)
)
)
echo Lines=%Lines%
echo Columns=%Columns%
For the actual window size refer to SomethingDark's answer (for how to use reg query
).
Upvotes: 1
Reputation: 14370
Both lines and columns (height and width, respectively) are found in the output of the mode con
command. Screen buffer size, unfortunately, is found in the registry and gets stored after being multiplied by 65535 for some reason.
@echo off
for /f "tokens=2" %%A in ('mode con ^| find "Lines"') do set "window_height=%%A"
for /f "tokens=2" %%A in ('mode con ^| find "Columns"') do set "window_width=%%A"
for /f "tokens=3" %%A in ('reg query HKCU\Console /v ScreenBufferSize') do set /a window_buffer=%%A/65535
echo Window is %window_width%x%window_height% with a buffer of %window_buffer%
If Lines shows the incorrect value for the window height, you might be able to use the WindowSize registry value of HKCU\Console:
for /f "tokens=3" %%A in ('reg query HKCU\Console /v WindowSize') do set /a window_height=%%A/65535
It is important to note that this is just the default height of the console and not the current height, but if the height was changed with mode con
, then the buffer size will be overwritten and the first way will work.
Upvotes: 1