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

后端 未结 14 1424
清歌不尽
清歌不尽 2020-12-02 09:59

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?

I know yo

相关标签:
14条回答
  • 2020-12-02 10:15
    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.

    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.

    0 讨论(0)
  • 2020-12-02 10:20

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

    An executable is included in 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;
    } 
    
    0 讨论(0)
  • 2020-12-02 10:24

    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

    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:

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

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

    0 讨论(0)
  • 2020-12-02 10:25

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

    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.

    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.WindowWidth and then set Console.BufferWidth because if you try to set Console.BufferWidth to a value less than the current Console.WindowWidth the program will throw an exception.

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

    0 讨论(0)
  • 2020-12-02 10:27

    As a workaround, you may use...

    Windows Powershell ISE

    As the Powershell script editor does not seems to have a buffer limitation in its read-eval-print-loop part (the "blue" part). And with Powershell you may execute DOS commands as well.

    PS. I understand this answer is a bit aside the original question, however I believe it is good to mention as it is a good workaround.

    0 讨论(0)
  • 2020-12-02 10:31

    There's a solution at CMD: Set buffer height independently of window height effectively 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.

    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.

    0 讨论(0)
提交回复
热议问题