How can I get the current width of the windows console in an environment variable within a batch file?
Click on the icon on the top left of the console frame (the one that looks like "C:\"), then select Properties . This lets you customize all kinds of stuff. The "layout" tab has the width of the window.
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment".
Machine environment variables are stored or retrieved from the following registry location: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment . Process environment variables are generated dynamically every time a user logs in to the device and are restricted to a single process.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With