Windows console width in environment variable

爱⌒轻易说出口 提交于 2020-01-27 08:17:27

问题


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


回答1:


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




回答2:


try this (language/locale/.net independent):

@ECHO OFF
SET "ConsoleWidth="
SET /A LINECOUNT=0
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2,*" %%A IN ('mode con') DO (SET /A LINECOUNT=!LINECOUNT!+1&IF !LINECOUNT! EQU 4 SET ConsoleWidth=%%B)
SETLOCAL DISABLEDELAYEDEXPANSION
SET "LINECOUNT="
ECHO ConsoleWidth: %ConsoleWidth% characters

tested on Windows XP and Windows 7, both in Czech language




回答3:


Powershell's (Get-Host).UI.RawUI.WindowSize property sets or returns the dimensions of the current console window. You can capture it with a for loop thusly:

for /f %%I in ('powershell ^(Get-Host^).UI.RawUI.WindowSize.width') do set width=%%I



回答4:


Alright, here's one that doesn't require powershell to be installed. It composes, runs and deletes a .Net application to set a batch script variable. :)

@echo off
setlocal
pushd "%windir%\microsoft.net\"
for /f "delims=" %%I in ('dir /s /b csc.exe') do (
    set csc=%%I
    goto next
)
:next
popd
echo using System;>width.cs
echo class Width {>>width.cs
echo public static void Main() {>>width.cs
echo string m1 = "{0}";>>width.cs
echo Console.WriteLine^(m1, Console.WindowWidth^); } }>>width.cs
"%csc%" /out:width.exe width.cs >NUL 2>NUL
for /f %%I in ('width.exe') do set width=%%I
del width.exe width.cs
echo %width%



回答5:


You can't get it in an environment variable, but it is stored in the registry so you can access it from your batch script.

There are answers here about how to change it: How to change Screen buffer size in Windows Command Prompt from batch script

In a similar way you can use reg.exe QUERY [key details] rather than reg.exe ADD [details]. See the Technet documentation for HKCU\Console for details.




回答6:


For a one simple line:

for /f tokens^=2 %%w in ('mode con^|find "Col"')do set _width=%%~w"



来源:https://stackoverflow.com/questions/14978548/windows-console-width-in-environment-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!