Windows console width in environment variable

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

+5
source share
5 answers

I like the approach using the built-in command modeon 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, not the window size (which scrolls).

Windows, Columns findstr Lines. , , ... , , 3000:)


, , findstr ... ( - ) 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

. Windows XP 3 (SP3) ( FAR).

+10

Powershell (Get-Host).UI.RawUI.WindowSize . for:

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

(//.net ):

@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

Windows XP Windows 7, .

+2

, , script.

, : Windows script

reg.exe QUERY [key details], reg.exe ADD [details]. . HKCU\Console.

0

Ok, here is one that does not require the installation of powershell. It creates, starts, and removes the .Net application to set the 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%
0
source

All Articles