如何从批处理脚本更改 Windows 命令提示符中的屏幕缓冲区大小
我知道您可以右键单击“属性”->“布局”,然后手动更改它。
但是如何从 Windows 批处理脚本更改它呢?
我知道您可以使用类似
MODE CON: COLS=90 LINES=10
的
脚本从脚本中更改它的大小,但是如何更改缓冲区大小呢?
该脚本将运行一段时间,有时在失败和退出之前需要一些时间,所以我需要更大的缓冲区。
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 you can change size of it from script using something like this
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我只是在寻找这个问题的答案,来找出命令本身调整缓冲区!
lines=70 部分实际上调整“屏幕缓冲区大小”设置中的高度,而不是“窗口大小”设置中的高度。
通过运行带有“lines=2500”设置(或任何您想要的缓冲区)的命令,然后检查窗口的“属性”,您会看到缓冲区确实设置为 2500,可以轻松证明。
我的批处理脚本最终看起来像这样:
I was just searching for an answer to this exact question, come to find out the command itself adjusts the buffer!
The lines=70 part actually adjusts the Height in the 'Screen Buffer Size' setting, NOT the Height in the 'Window Size' setting.
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.
My batch script ends up looking like this:
我只是尝试在 Windows 7 上设置最大行数,我可以使用 mode con 命令进行设置,发现它是 32766 2^15-2,
尽管您可以设置 screen,但 您可以使用以下命令进行设置也可以从 GUI 获取缓冲区大小,但您可以获得的最大值是 9999。
I was just giving a try for max lines on windows 7 i can set using
mode con
command and found it to be 32766 2^15-2 and you can set it with following commandalthough you can set screen buffer size from the GUI too, but the max you can get is 9999.
设置缓冲区,但也将窗口高度增加到全屏,这很难看。
您可以直接在注册表中更改设置:
您启动的下一个cmd.exe会增加缓冲区。
因此,这不适用于您已经使用的 cmd.exe,但只需在 pre-batch.cmd 中使用它即可调用您的主脚本。
sets the buffer, but also increases the window height to full screen, which is ugly.
You can change the settings directly in the registry :
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.
下面是一个非常简单的 VB.NET 程序,它可以完成您想要的操作。
它将缓冲区设置为 100 个字符宽 x 1000 个字符高。然后它设置窗口的宽度以匹配缓冲区大小。
更新
我修改了代码以首先设置
Console.WindowWidth
然后设置Console.BufferWidth
因为如果您尝试将Console.BufferWidth
设置为一个值小于当前的Console.WindowWidth
程序将抛出异常。这只是一个示例...您应该添加代码来处理命令行参数和错误处理。
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.
UPDATE
I modified the code to first set
Console.WindowWidth
and then setConsole.BufferWidth
because if you try to setConsole.BufferWidth
to a value less than the currentConsole.WindowWidth
the program will throw an exception.This is only a sample...you should add code to handle command line parameters and error handling.
CMD:设置缓冲区高度与窗口高度无关 有效地使用从批处理脚本执行的 powershell 命令。这个解决方案让我能够独立于窗口大小调整现有批处理脚本窗口中的回滚缓冲区的大小,这正是OP所要求的。
警告:它似乎使脚本忘记变量(或者至少在我的脚本中如此),所以我建议仅在脚本的开头和/或结尾处调用该命令,或者在不依赖于会话的其他地方调用该命令局部变数。
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.
这是我所做的,以防有人发现它有用(我根据本线程中的其余答案使用了很多东西):
首先,我根据需要调整了布局设置。这会立即更改窗口大小,因此更容易设置我想要的确切大小。 顺便说一句,我不知道我还可以拥有小于宽度缓冲区的可见宽度。这很好,因为我通常讨厌换行很长。
然后单击“确定”后,我打开 regedit.exe 并转到“HKEY_CURRENT_USER\Console”。那里有一个新的子条目“%SystemRoot%_system32_cmd.exe”。我右键单击它并选择导出:
我将其另存为“cmd_settings.reg”。然后,我创建了一个导入这些设置的批处理脚本,调用我的原始批处理脚本(名称batch_script.bat),然后删除我导入的内容,以便命令行窗口返回到默认设置:
这是一个可以调用的示例批处理(“batch_script.bat”):
如果您希望在脚本执行后运行 reg delete 行,请不要忘记脚本末尾的 exit 命令。
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.
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:
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:
This is a sample batch that could be invoked ("batch_script.bat"):
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.
没有“DOS 命令提示符”。 DOS 在 Windows ME 中完全消失 (7 /11/2006)。它在 Windows NT(NT、2K、XP、Vista、7)上简称为命令提示符。
无法通过内置 cmd.exe 命令更改屏幕缓冲区。它可以通过控制台 API 函数进行更改,因此您也许能够创建一个实用程序来修改它。我自己从来没有尝试过这个。
另一个建议是将输出重定向到文件和屏幕,以便您拥有它的“硬拷贝”。 Windows 没有像 Unix 那样的 TEE 命令,但是有人已经解决了这个问题。
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).
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.
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.
我创建了以下实用程序来设置控制台大小和滚动缓冲区。
我使用 DEV C++ 编译它(http://www.bloodshed.net/devcpp.html) 。
可执行文件包含在 https://sourceforge.net/projects/wa2l-wintools/ 中。
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/.
如果有人仍然想知道,在较新版本的 Windows 上,您可以使用 powershell:
If anyone is still wondering, on the newer versions of windows you can use powershell:
您可以通过点击左上角的图标-->属性-->更改cmd的缓冲区大小。布局-->屏幕缓冲区大小。
您甚至可以使用 cmd 命令更改它
模式 con:cols=100 行=60
直到第 58 行,cmd 窗口的高度发生变化..
在第 58 行值之后,cmd 的缓冲区大小发生变化...
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...
我知道这个问题已有 9 年历史了,但也许有人会对它更感兴趣。根据问题,要仅更改缓冲区大小,可以使用Powershell。
Powershell 最短的命令是:
将宽度和高度的值替换为您想要的值。
但据推测您问题的原因是修改命令行窗口的大小而不减少缓冲区大小。为此,您可以使用以下命令:
在那里您可以单独修改缓冲区和窗口的大小。为了避免出现错误消息,请考虑 buffersize 的值必须大于或等于 windowsize 的值。
另外实现的是
start /b
命令。 Powershell 命令有时需要几秒钟才能执行。通过这个附加命令,Powershell 可以并行运行,并且不会显着延迟以下命令的处理。I know the question is 9 years old, but maybe for someone it will be interested furthermore. According to the question, to change the buffer size only, it can be used Powershell.
The shortest command with Powershell is:
Replace the values of width and height with your wanted values.
But supposedly the reason of your question is to modify the size of command line window without reducing of the buffer size. For that you can use the following command:
There you can modify the size of buffer and window independly. To avoid an error message, consider that the values of buffersize must be bigger or equal to the values of windowsize.
Additional implemented is the
start /b
command. The Powershell command sometimes takes a few seconds to execute. With this additional command, Powershell runs parallel and does not significantly delay the processing of the following commands.我正在扩展我在这里发布的评论,因为大多数人不会注意到该评论。
https://lifeboat.com/programs/console.exe 是 Visual Studio 的编译版本基本程序参见https://stackoverflow.com/a/4694566/1752929。我的版本只是将缓冲区高度设置为 32766,这是可用的最大缓冲区高度。它不会调整任何其他内容。如果有很多需求,我可以创建一个更灵活的程序,但通常您可以在快捷布局选项卡中设置其他变量。
以下是我在快捷方式中使用的目标,我希望从 f 目录开始。 (我必须以这种方式设置目录,因为如果您希望以管理员身份运行命令提示符,Windows 不会让您以任何其他方式设置它。)
I'm expanding upon a comment that I posted here as most people won't notice the comment.
https://lifeboat.com/programs/console.exe is a compiled version of the Visual Basic program described at https://stackoverflow.com/a/4694566/1752929. My version simply sets the buffer height to 32766 which is the maximum buffer height available. It does not adjust anything else. If there is a lot of demand, I could create a more flexible program but generally you can just set other variables in your shortcut layout tab.
Following is the target I use in my shortcut where I wish to start in the f directory. (I have to set the directory this way as Windows won't let you set it any other way if you wish to run the command prompt as administrator.)
作为解决方法,您可以使用...
Windows Powershell ISE
由于 Powershell 脚本编辑器的 read-eval-print-loop 部分(“蓝色”部分)似乎没有缓冲区限制。使用 Powershell,您还可以执行 DOS 命令。
附言。我知道这个答案有点偏离原来的问题,但我相信值得一提,因为它是一个很好的解决方法。
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.
我找到了一种在不影响窗口大小的情况下调整缓冲区大小的方法。它的工作原理归功于批处理工作方式的缺陷,但它完成了工作。
它是如何运作的?该命令有语法错误,应该是“mode 648, 78”。由于批处理的工作方式,缓冲区大小将首先调整为 648,然后窗口大小调整将会到来,但由于语法错误,它永远不会完成。瞧,缓冲区大小已调整,窗口大小保持不变。这会产生一个丑陋的错误,因此要消除它,只需添加“>nul 2>nul”即可。
I have found a way to resize the buffer size without influencing the window size. It works thanks to a flaw in how batch works but it gets the job done.
How does it work? There is a syntax error in this command, it should be "mode 648, 78". Because of how batch works, the buffer size will first be resized to 648 and then the window resize will come but it will never finish, because of the syntax error. Voila, buffer size is adjusted and the window size stays the same. This produces an ugly error so to get rid of it just add the ">nul 2>nul" and you're done.