Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get screen width (in characters) in batch file?

115 views
Skip to first unread message

Kenny McCormack

unread,
Nov 18, 2020, 7:48:58 PM11/18/20
to
In a batch file, I want to get the width of the screen (e.g., 80, in the
normal case) into a variable. This is, e.g., the number displayed in the
"Properties" of the Command Prompt window - as the "screen width".

I know how to get this is in a regular programming language (e.g., C or
C++), using a Win32 API call, but I don't know of any easy way to get it
purely in batch (CMD.EXE).

Also, not interested in any solutions that involve things like PowerShell
or VBA. As I said, if it requires going to a regular programming language,
I already know how to do that.

Any ideas?

--
"The party of Lincoln has become the party of John Wilkes Booth."

- Carlos Alazraqui -

Zaidy036

unread,
Nov 18, 2020, 8:03:15 PM11/18/20
to
On 11/18/2020 7:48 PM, Kenny McCormack wrote:
> In a batch file, I want to get the width of the screen (e.g., 80, in the
> normal case) into a variable. This is, e.g., the number displayed in the
> "Properties" of the Command Prompt window - as the "screen width".
>
> I know how to get this is in a regular programming language (e.g., C or
> C++), using a Win32 API call, but I don't know of any easy way to get it
> purely in batch (CMD.EXE).
>
> Also, not interested in any solutions that involve things like PowerShell
> or VBA. As I said, if it requires going to a regular programming language,
> I already know how to do that.
>
> Any ideas?
>
MODE CON: COLS=xx LINES=yy

Kenny McCormack

unread,
Nov 18, 2020, 8:58:27 PM11/18/20
to
In article <rp4g8i$nmd$1...@dont-email.me>,
I think you misread the word "get" as "set".

--
Conservatives want smaller government for the same reason criminals want fewer cops.

Kerr-Mudd,John

unread,
Nov 19, 2020, 7:30:00 AM11/19/20
to
Well, the clue is there; parse the output of "mode con:" for "Columns:"

--
Bah, and indeed, Humbug.

Zaidy036

unread,
Nov 19, 2020, 10:39:04 AM11/19/20
to
MODE > [file]
FIND "Columns" [file] > [file 2]
SET /P _Col=<[file 2]
SET _Col=%_Col:Columns: =%
etc

Kerr-Mudd,John

unread,
Nov 19, 2020, 11:43:14 AM11/19/20
to
On Thu, 19 Nov 2020 00:48:56 GMT, gaz...@shell.xmission.com (Kenny
McCormack) wrote:

> In a batch file, I want to get the width of the screen (e.g., 80, in
> the normal case) into a variable. This is, e.g., the number
> displayed in the "Properties" of the Command Prompt window - as the
> "screen width".
>
> I know how to get this is in a regular programming language (e.g., C
> or C++), using a Win32 API call, but I don't know of any easy way to
> get it purely in batch (CMD.EXE).
>
> Also, not interested in any solutions that involve things like
> PowerShell or VBA. As I said, if it requires going to a regular
> programming language, I already know how to do that.
>
> Any ideas?
>
Sure. What's my fee?

Kenny McCormack

unread,
Nov 19, 2020, 2:03:30 PM11/19/20
to
In article <XnsAC7A7F2789...@144.76.35.198>,
Kerr-Mudd,John <nots...@127.0.0.1> wrote:
...
>>> MODE CON: COLS=xx LINES=yy
>> I think you misread the word "get" as "set".
> Well, the clue is there; parse the output of "mode con:" for "Columns:"

Indeed. Thanks.

"mode con" delivers the goods.

--
People who want to share their religious views with you
almost never want you to share yours with them. -- Dave Barry

Herbert Kleebauer

unread,
Nov 19, 2020, 3:39:47 PM11/19/20
to
On 19.11.2020 16:39, Zaidy036 wrote:

> MODE > [file]
> FIND "Columns" [file] > [file 2]
> SET /P _Col=<[file 2]
> SET _Col=%_Col:Columns: =%
> etc
>

@echo off
for /f "tokens=2" %%i in ('mode con:^|find "Columns"') do set /a n=%%i
echo %n%


Kerr-Mudd,John

unread,
Nov 19, 2020, 5:06:51 PM11/19/20
to
On Thu, 19 Nov 2020 16:43:13 GMT, "Kerr-Mudd,John" <nots...@127.0.0.1>
wrote:
Sorry, I was a bit grumpy earlier;

@echo off
rem mode con: cols=97
for /F "tokens=1,2 delims= " %%i in ('mode con:') do if %%i==Columns:
echo cols=%%j

Kerr-Mudd,John

unread,
Nov 20, 2020, 7:41:55 AM11/20/20
to
On Thu, 19 Nov 2020 22:06:50 GMT, "Kerr-Mudd,John" <notsaying@
I'm also a bit rusty; no need for the 'delims= '

for /F "tokens=1,2" %i in ('mode con:') do if %i == Columns:
set cols=%j

mokomoji

unread,
Jan 25, 2021, 5:00:38 AM1/25/21
to
2020년 11월 19일 목요일 오전 9시 48분 58초 UTC+9에 Kenny McCormack님이 작성한 내용:
@echo off
setlocal
cd /d "%~dp0"
chcp 437
cls
for /f "tokens=1* delims=: " %%f in ('mode con') do (
call set /a z_num+=1
call set "z_mode_%%z_num%%=%%~g"
)
echo lines=%z_mode_3%
echo colums=%z_mode_4%
:end
endlocal
pause

0 new messages