Windows 批处理文件的文件大小和行长限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1217134/
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
File-size and line-length limits for Windows batch files
提问by zedoo
I am generating a Windows batch file that might become quite large, say a few megabytes. I've searched for possible limits regarding the maximum file size and maximum length of a line in a batch file, but I couldn't find anything. Any practical experiences?
我正在生成一个 Windows 批处理文件,它可能会变得非常大,比如几兆字节。我在批处理文件中搜索了有关最大文件大小和最大行长度的可能限制,但找不到任何内容。有什么实际经验吗?
采纳答案by schnaader
I think filesize can be anything up to 2 GB, perhaps even more. It's an interpreted language, so if this is done right, filesize limit should be the filesize limit of the file system. I never had any errors with batch files being too large, and some of those I created were several MBs in size.
我认为文件大小可以高达 2 GB,甚至更多。它是一种解释性语言,所以如果这样做正确,文件大小限制应该是文件系统的文件大小限制。我从来没有因为批处理文件过大而出现任何错误,我创建的其中一些文件大小有几 MB。
There should be a line length limit, but it should be more than 256. This can easily be tested, just do some "set A=123456789012...endofline", after that "echo %A%", and you'll see how far you can go.
应该有一个行长限制,但它应该超过 256。这很容易测试,只需做一些“set A=123456789012...endofline”,然后“echo %A%”,你就会看到你能走多远。
It works for me with very long lines (around 4K), but at 8K echo
gives a message, "Line too long", so 8192 bytes should be some limit.
它适用于很长的行(大约 4K),但在 8K 时echo
会给出一条消息,“行太长”,所以 8192 字节应该是一些限制。
Now tested for filesize, too, tested with "echo off", thousands of set lines, after that "echo end", and it worked for a 11 MB file (although it took some seconds to finish :) - no limit in sight here.
现在也测试了文件大小,在“回声结束”之后测试了“回声关闭”,数千行设置,并且它适用于 11 MB 的文件(尽管需要几秒钟才能完成:) - 这里没有限制.
110 MB worked, too. Is this enough? ;)
110 MB 也有效。这够了吗?;)
回答by SS64
The maximum length of any command line (or variable) within CMD is 8191 characters.
CMD 中任何命令行(或变量)的最大长度为8191 个字符。
回答by jeb
There are many different limits.
有许多不同的限制。
There is no (known) limit for the file itself, also code blocks seems to be unlimited.
文件本身没有(已知的)限制,代码块似乎也没有限制。
The maximal size of a variable is 8191 characters.
Obviously the limit for a line has to be larger to assign a maximal sized variable.
Even the variable name can be 8191 chars long.
变量的最大大小为 8191 个字符。
显然,一行的限制必须更大才能分配最大大小的变量。
甚至变量名也可以是 8191 个字符长。
It's possible to build a line with 16387 chars
可以用 16387 个字符构建一条线
set <varname_8191_chars_long>=<content_8191_chars_long>
But the batch parser is able to read much longer lines (tested with 100k in one line).
But it's required, that the effective length is < ~8191 chars after the percent expansion.
Like in
但是批处理解析器能够读取更长的行(在一行中使用 100k 进行测试)。
但需要的是,有效长度在百分比扩展后 < ~8191 个字符。
像
echo %======%%=====%%======%%=====% ..... %=====%END
This works because after the expansion the line contains only echo END
.
( One bugof the parser: The parser drops at every multiple of 8192 one character)
这是有效的,因为在扩展后该行只包含echo END
.
(解析器的一个bug:解析器每8192的倍数就掉一个字符)
回答by Cheeso
Some ideas, not necessarily mutually exclusive:
一些想法,不一定相互排斥:
- Switch to PowerShell.
- Switch to a data-driven application, so that all the variable stuff is kept in a data file (CSV, text, whatever), and as a result you can have a smaller, boilerplate script that opens the data file and operates.
- 切换到PowerShell。
- 切换到数据驱动的应用程序,以便所有变量都保存在数据文件(CSV、文本等)中,因此您可以拥有一个较小的样板脚本,用于打开数据文件并进行操作。
回答by Peter Mortensen
It should work at least up to 2 GB. The lines are read directly from the BAT file on the disk (there is no caching involved). I make this statement because of the following:
它应该至少工作到 2 GB。这些行是直接从磁盘上的 BAT 文件中读取的(不涉及缓存)。我做出这个声明是因为以下几点:
In fact you can edit a BAT file while it is running! And it will work even though a text editor may rename the original version and save the new version in a new location on the disk. As long as you are careful not to insert text above the currently executing command. Lines can be changed/inserted/deleted below the currently executing command and the new lines will be the ones executed. I have often done this with BAT files containing a long list of wget commands, each taking tens of minutes to execute.
事实上,您可以在运行时编辑 BAT 文件!即使文本编辑器可能会重命名原始版本并将新版本保存在磁盘上的新位置,它也能正常工作。只要您注意不要在当前执行的命令上方插入文本。可以在当前执行的命令下方更改/插入/删除行,新行将被执行。我经常使用包含一长串 wget 命令的 BAT 文件来完成此操作,每个命令都需要数十分钟来执行。
回答by DanielM
According to Microsoft, there is no limit to a batch file size. However, a batch file line should not exceed 127 bytes or it will truncated at execution.
根据微软的说法,批处理文件的大小没有限制。但是,批处理文件行不应超过 127 字节,否则将在执行时被截断。
See Maximum Line Length and Count for Batch Files & CONFIG.SYS