Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows console width in environment variable

How can I get the current width of the windows console in an environment variable within a batch file?

like image 597
Axel Fontaine Avatar asked Feb 20 '13 11:02

Axel Fontaine


People also ask

How do I resize a console window?

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.

How to set environment variable in windows registry?

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".

Where are environment variables in the registry?

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.


1 Answers

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).

like image 196
icabod Avatar answered Oct 07 '22 10:10

icabod