I like the approach using the built-in mode
command in Windows.
Try the following batch-file:
@echo off
for /F "usebackq tokens=2* delims=: " %%W in (`mode con ^| findstr Columns`) do set CONSOLE_WIDTH=%%W
echo Console is %CONSOLE_WIDTH% characters wide
Note that this will return the size of the console buffer, and not the size of the window (which is scrollable).
If you wanted the height of the windows console, you can replace Columns
in the findstr
expression with Lines
. Again, it will return the height of the buffer, not the window... I personally like to have a big buffer to allow scrolling back through history, so for me the Lines usually reports about 3000 :)
Just for fun, here's a version that doesn't use findstr
to filter the output... in case (for some reason) you have a dislike of findstr
:
@echo off
for /F "usebackq tokens=1,2* delims=: " %%V in (`mode con`) do (
if .%%V==.Columns (
set CONSOLE_WIDTH=%%W
goto done
)
)
:done
echo Console is %CONSOLE_WIDTH% characters wide
Note, this was all tried in Windows XP SP3, in a number of different windows (including one executing FAR manager).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…