本文介绍了环境变量中的 Windows 控制台宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在批处理文件的环境变量中获取 Windows 控制台的当前宽度?

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

推荐答案

我喜欢在 Windows 中使用内置 mode 命令的方法.试试下面的批处理文件:

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

如果您想要 Windows 控制台的高度,可以将 findstr 表达式中的 Columns 替换为 Lines.同样,它将返回缓冲区的高度,而不是窗口...我个人喜欢有一个大缓冲区以允许回滚历史,所以对我来说,Lines 通常报告大约 3000 :)

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

只是为了好玩,这是一个不使用 findstr 来过滤输出的版本...以防万一(出于某种原因)您不喜欢 findstr:

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

注意,这一切都在 Windows XP SP3 中尝试过,在许多不同的窗口中(包括一个正在执行的 FAR 管理器).

Note, this was all tried in Windows XP SP3, in a number of different windows (including one executing FAR manager).

这篇关于环境变量中的 Windows 控制台宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 01:39