如何从批处理脚本更改 Windows 命令提示符中的屏幕缓冲区大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4692673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:58:30  来源:igfitidea点击:

How to change Screen buffer size in Windows Command Prompt from batch script

windowsbatch-filecmd

提问by grobartn

I know you can do right click properties ->layout and there change it manually.

我知道您可以右键单击属性-> 布局,然后手动更改它。

But how would you go about changing it from a Windows batch script?

但是您将如何从 Windows 批处理脚本更改它呢?

I know you can change size of it from script using something like this

我知道你可以使用这样的东西从脚本中改变它的大小

MODE CON: COLS=90 LINES=10

MODE CON: COLS=90 LINES=10

But how can you change buffer size?

但是如何更改缓冲区大小呢?

The script will run for a while and sometimes before failing and exiting it takes some time, so I need larger buffer.

脚本会运行一段时间,有时在失败和退出之前需要一些时间,所以我需要更大的缓冲区。

回答by Namuna

I was just searching for an answer to this exact question, come to find out the command itself adjusts the buffer!

我只是在寻找这个确切问题的答案,来找出命令本身调整缓冲区!

mode con:cols=140 lines=70

The lines=70part actually adjusts the Height in the 'Screen Buffer Size' setting, NOT the Height in the 'Window Size' setting.

线= 70部分实际上调整在“屏幕缓冲区大小”的高度设置,而不是在“窗口大小”设置的高度。

Easily proven by running the command with a setting for 'lines=2500' (or whatever buffer you want) and then check the 'Properties' of the window, you'll see that indeed the buffer is now set to 2500.

通过使用设置为“lines=2500”(或您想要的任何缓冲区)的命令运行命令,然后检查窗口的“属性”,您将看到缓冲区现在确实设置为 2500。

My batch script ends up looking like this:

我的批处理脚本最终看起来像这样:

@echo off
cmd "mode con:cols=140 lines=2500"

回答by Sankdyl

I was just giving a try for max lines on windows 7 i can set using mode concommand and found it to be 32766 2^15-2 and you can set it with following command

我只是尝试在 Windows 7 上设置最大行数,我可以使用mode con命令进行设置,发现它是 32766 2^15-2,您可以使用以下命令进行设置

mode con lines=32766

although you can set screen buffer size from the GUI too, but the max you can get is 9999.

虽然您也可以从 GUI 设置屏幕缓冲区大小,但您可以获得的最大值为 9999。

回答by Peter Hahndorf

mode con lines=32766

sets the buffer, but also increases the window height to full screen, which is ugly.

设置缓冲区,但也将窗口高度增加到全屏,这很丑陋。

You can change the settings directly in the registry :

您可以直接在注册表中更改设置:

:: escape the environment variable in the key name
set mySysRoot=%%SystemRoot%%

:: 655294544 equals 9999 lines in the GUI
reg.exe add "HKCU\Console\%mySysRoot%_system32_cmd.exe" /v ScreenBufferSize /t REG_DWORD /d 655294544 /f

:: We also need to change the Window Height, 3276880 = 50 lines
reg.exe add "HKCU\Console\%mySysRoot%_system32_cmd.exe" /v WindowSize /t REG_DWORD /d 3276880 /f

The next cmd.exe you start has the increase buffer.

您启动的下一个 cmd.exe 具有增加缓冲区。

So this doesn't work for the cmd.exe you are already in, but just use this in a pre-batch.cmd which than calls your main script.

所以这对你已经在的 cmd.exe 不起作用,但只需在 pre-batch.cmd 中使用它,而不是调用你的主脚本。

回答by aphoria

Below is a very simple VB.NET program that will do what you want.

下面是一个非常简单的 VB.NET 程序,它可以满足您的需求。

It will set the buffer to 100 chars wide by 1000 chars high. It then sets the width of the window to match the buffer size.

它将缓冲区设置为 100 个字符宽 x 1000 个字符高。然后它设置窗口的宽度以匹配缓冲区大小。

Module ConsoleBuffer
  Sub Main()
    Console.WindowWidth = 100
    Console.BufferWidth = 100
    Console.BufferHeight = 1000
  End Sub
End Module

UPDATE

更新

I modified the code to first set Console.WindowWidthand then set Console.BufferWidthbecause if you try to set Console.BufferWidthto a value less than the current Console.WindowWidththe program will throw an exception.

我将代码修改为先设置Console.WindowWidth然后设置,Console.BufferWidth因为如果您尝试设置Console.BufferWidth为小于当前Console.WindowWidth的值,程序将抛出异​​常。

This is only a sample...you should add code to handle command line parameters and error handling.

这只是一个示例……您应该添加代码来处理命令行参数和错误处理。

回答by rojo

There's a solution at CMD: Set buffer height independently of window heighteffectively employing a powershell command executed from the batch script. This solution let me resize the scrollback buffer in the existing batch script window independently of the window size, exactly what the OP was asking for.

CMD有一个解决方案有效地使用从批处理脚本执行的 powershell 命令,独立于窗口高度设置缓冲区高度。这个解决方案让我可以独立于窗口大小调整现有批处理脚本窗口中回滚缓冲区的大小,这正是 OP 所要求的。

Caveat: It seems to make the script forget variables (or at least it did with my script), so I recommend calling the command only at the beginning and / or end of your script, or otherwise where you don't depend on a session local variable.

警告:这似乎使脚本忘记了变量(或者至少它在我的脚本中忘记了),因此我建议仅在脚本的开头和/或结尾调用该命令,或者在不依赖会话的其他地方调用该命令局部变量。

回答by xtsoler

Here's what I did in case someone finds it useful (I used a lot of things according to the rest of the answers in this thread):

这是我所做的,以防有人发现它有用(根据本线程中的其余答案,我使用了很多东西)

First I adjusted the layout settings as needed. This changes the window size immediately so it was easier to set the exact size that I wanted. By the way I didn't know that I can also have visible width smaller than width buffer. This was nice since I usually hate a long line to be wrapped.enter image description here

首先,我根据需要调整了布局设置。这会立即更改窗口大小,因此更容易设置我想要的确切大小。顺便说一句,我不知道我的可见宽度也可以小于宽度缓冲区。这很好,因为我通常讨厌排长队。在此处输入图片说明

Then after clicking ok, I opened regedit.exe and went to "HKEY_CURRENT_USER\Console". There is a new child entry there "%SystemRoot%_system32_cmd.exe". I right clicked on that and selected Export: enter image description here

然后单击确定后,我打开 regedit.exe 并转到“HKEY_CURRENT_USER\Console”。有一个新的子条目“%SystemRoot%_system32_cmd.exe”。我右键单击它并选择导出: 在此处输入图片说明

I saved this as "cmd_settings.reg". Then I created a batch script that imports those settings, invokes my original batch script (name batch_script.bat) and then deletes what I imported in order for the command line window to return to default settings:

我将其保存为“cmd_settings.reg”。然后我创建了一个导入这些设置的批处理脚本,调用我的原始批处理脚本(名称 batch_script.bat),然后删除我导入的内容,以便命令行窗口返回到默认设置:

regedit /s cmd_settings.reg
start /wait batch_script.bat
reg delete "HKEY_CURRENT_USER\Console\%%SystemRoot%%_system32_cmd.exe" /f

This is a sample batch that could be invoked ("batch_script.bat"):

这是一个可以调用的示例批次(“batch_script.bat”):

@echo off
echo test!
pause
exit

Don't forget the exit command at the end of your script if you want the reg delete line to run after the script execution.

如果您希望在脚本执行后运行 reg delete 行,请不要忘记脚本末尾的 exit 命令。

回答by Tergiver

There is no "DOS command prompt". DOS fully died with Windows ME (7/11/2006). It's simply called the Command Prompt on Windows NT (which is NT, 2K, XP, Vista, 7).

没有“DOS 命令提示符”。DOS 在 Windows ME 中完全消失(2006 年 7 月 11 日)。它在 Windows NT(NT、2K、XP、Vista、7)上简称为命令提示符。

There is no way to alter the screen buffer through built-in cmd.exe commands. It can be altered through Console API Functions, so you might be able to create a utility to modify it. I've never tried this myself.

无法通过内置的 cmd.exe 命令更改屏幕缓冲区。它可以通过Console API Functions进行更改,因此您可以创建一个实用程序来修改它。我自己从来没有尝试过这个。

Another suggestion would be to redirect output to both a file and to the screen so that you have a "hard copy" of it. Windows does not have a TEE command like Unix, but someone has remedied that.

另一个建议是将输出重定向到文件和屏幕,以便您拥有它的“硬拷贝”。Windows 没有像 Unix 那样的 TEE 命令,但有人已经解决了这个问题

回答by CWa

I created the following utility to set the console size and scroll buffers.

我创建了以下实用程序来设置控制台大小和滚动缓冲区。

I compiled it using DEV C++ (http://www.bloodshed.net/devcpp.html).

我使用 DEV C++ ( http://www.bloodshed.net/devcpp.html)编译它。

An executable is included in https://sourceforge.net/projects/wa2l-wintools/.

可执行文件包含在https://sourceforge.net/projects/wa2l-wintools/ 中

#include <iostream>
#include <windows.h> 

using namespace std;


// SetWindow(Width,Height,WidthBuffer,HeightBuffer) -- set console size and buffer dimensions
//
void SetWindow(int Width, int Height, int WidthBuffer, int HeightBuffer) { 
    _COORD coord; 
    coord.X = WidthBuffer; 
    coord.Y = HeightBuffer; 

    _SMALL_RECT Rect; 
    Rect.Top = 0; 
    Rect.Left = 0; 
    Rect.Bottom = Height - 1; 
    Rect.Right = Width - 1; 

    HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle 
    SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size 
    SetConsoleWindowInfo(Handle, TRUE, &Rect);            // Set Window Size 
}  // SetWindow



// main(Width,Height,WidthBuffer,HeightBuffer) -- main
//
int main(int argc, char *argv[]) {     
    int width = 80;
    int height = 25;
    int wbuffer = width + 200;
    int hbuffer = height + 1000;

    if ( argc == 5 ){
        width = atoi(argv[1]);
        height = atoi(argv[2]);
        wbuffer = atoi(argv[3]);
        hbuffer = atoi(argv[4]);
    } else if ( argc > 1 ) {
        cout << "Usage: " << argv[0] << " [ width height bufferwidth bufferheight ]" << endl << endl;
        cout << "  Where" << endl;
        cout << "    width            console width" << endl;
        cout << "    height           console height" << endl;
        cout << "    bufferwidth      scroll buffer width" << endl;
        cout << "    bufferheight     scroll buffer height" << endl;
        return 4;
    }    

    SetWindow(width,height,wbuffer,hbuffer);
    return 0;
} 

回答by Arrow Pulford

If anyone is still wondering, on the newer versions of windows you can use powershell:

如果有人仍然想知道,在较新版本的 Windows 上,您可以使用 powershell:

powershell.exe -command "& {$pshost = Get-Host;$pswindow = $pshost.UI.RawUI;$newsize = $pswindow.BufferSize;$newsize.height = 150;$pswindow.buffersize = $newsize;}"

回答by Tejeshreddy

You can change the buffer size of cmd by clicking the icon at top left corner -->properties --> layout --> screen buffer size.
you can even change it with cmd command

mode con:cols=100 lines=60
Upto lines = 58 the height of the cmd window changes ..
After lines value of 58 the buffer size of the cmd changes...

您可以通过单击左上角的图标 --> 属性 --> 布局 --> 屏幕缓冲区大小来更改 cmd 的缓冲区大小。
您甚至可以使用 cmd 命令更改它

mode con:cols=100 lines=60
Upto lines = 58 cmd 窗口的高度会发生变化..
行值为 58 后,cmd 的缓冲区大小会发生变化...