batch-file - 在批处理脚本中,标签和标签后面出现的文本是否有记录的长度限制?

标签 batch-file windows-10 string-length

考虑以下标签,并在其后添加文本,以阐明批处理脚本中参数的用途(空格后的任何内容都将被忽略......):

:LineBuilder [1 - main var type 1] [2 - main var type 2] [3 - subvar type] [4 - string to store in 1.3] [5 - Option for giving the length vs needing to calculate it] [6 - var containing Width to pull from] [7 - var containing Blanks to pull from] [8 - Option to add CRLF at the end of string] [9 - Border Option] [10 - Option to justify text 1/left, 2/right, 3/split - right text supplied Arg4, 4/centered, 5/split - left text supplied Arg5] [11 - Left portion of Text if Justy is split [spaces in between text]]

引用此SO Article ,我知道 Label 的最大长度本身是128个字符。是否还记录了标签之后文本的长度可以有多长?如果没有正式记录,或者这个特定问题尚未得到解答,我可能有答案,但想确认它没有在其他地方列出。

不包括冒号( : ),整行有 511 个字符。之后的字符串长度,包括所有空格(以及将 arg clarificationsLabel 本身分隔开的空格)为 500 个字符长。我最终是如何偶然发现这个错误的,我花了一段时间才查明!看来 CMD Line Interpreter 放弃了存储 Label's 的尝试。字符串数据不再在一个字符串中,并尝试解析最终的 ]作为命令! (如果您在第一个位置后添加 REM ,错误将更改为 ext]] - 正好增加 4 个字符。

']' is not recognized as an internal or external command,
operable program or batch file.

在 Windows 10 中体验这一点。

更新

根据下面jeb的回答以及评论中的问题,我可以证明该文件保存为 CR LF行结尾(如 NotePad++ 中所示进行验证,编码的 UTF-8 (这有什么区别吗?)....

显示其使用位置的最小示例:

REM <<<-_-_-_-_-_END :OtherRoutine_-_-_-_-_->>>
 REM Description Box 
 
 REM Other "SubRoutine Code"

 Call :LineBuilder "consolText" "consol" "lineOne" "Searching for Corrupted Files.  Please wait." "44" "consol.width" "consol.blanks" "1" "0" "1" ""

 REM Other "SubRoutine Code"
 Goto :EOF
REM <<<-_-_-_-_-_END :OtherRoutine_-_-_-_-_->>>

REM ╔══════════════════════════════════════════════════════╗
REM ║  -LineBuilder-                                       ║
REM ║Sets the variable for specified line to equal the text║
REM ║supplied, takes the line variable, gets the string    ║
REM ║length, and then calls ComputeLineLen to establish the║
REM ║total console's line length to set the blanks. Then it║
REM ║sets the line's blanks length and establishes the     ║
REM ║fully computed line for output in MainPrompt, with    ║
REM ║the spaces justified left, right, split, or centered. ║
REM ╚══════════════════════════════════════════════════════╝

REM <<<-_-_-_-_-_BEGIN :LineBuilder_-_-_-_-_->>>
 :LineBuilder [1 - main var type 1] [2 - main var type 2] [3 - subvar type] [4 - string to store in 1.3] [5 - Option for giving the length vs needing to calculate it] [6 - var containing Width to pull from] [7 - var containing Blanks to pull from] [8 - Option to add CRLF at the end of string] [9 - Border Option] [10 - Option to justify text 1/left, 2/right, 3/split - right text supplied Arg4, 4/centered, 5/split - left text supplied Arg5] [11 - Left portion of Text if Justy is split [spaces in between text]]
 
 <nul (set /p "lineOut= LineBuilder:!cr!!lf!")>>%log%
 <nul (set /p "lineOut= LineBuilder:!cr!!lf!")>>%formLog%

 REM More Code
 Goto :EOF
REM <<<-_-_-_-_-_END :LineBuilder_-_-_-_-_->>>

如果我将标签后的文本更改为以下内容,错误就会消失:

 :LineBuilder [1 - main var type 1] [2 - main var type 2] [3 - subvar type] [4 - string to store in 1.3] [5 - Option for giving the length vs needing to calculate it] [6 - var containing Width to pull from] [7 - var containing Blanks to pull from] [8 - Option to add CRLF at end of string] [9 - Border Option] [10 - Option to justify text 1/left, 2/right, 3/split - right text supplied Arg4, 4/centered, 5/split - left text supplied Arg5] [11 - Left/right Text if Justy is split [spaces in text]]

最佳答案

长标签行的最小示例:

@echo off
goto :myLabel

:myLabel xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12345678

输出“345678”未被识别为内部或外部命令、可操作程序或批处理文件。

在标签行中,文本“1234578”在 512 个前导字符(冒号标签名称和许多 x)之后开始。

标签解析器可能有一个 512 字节的缓冲区(CRLF +2),当该缓冲区中没有找到 CRLF 时,它只是停止搜索 CRLF 并只使用以下字符。

规则是:如果标签行长度超过 514 字节,解析器将简单地假定下一个命令位于位置 514。

顺便说一句,您应该通过将描述移动到下一行来解决您的问题。这比超长的一行更具可读性。

:LineBuilder 
::: [1 - main var type 1] 
::: [2 - main var type 2]
::: [3 - subvar type] 
::: [4 - string to store in 1.3] 
::: [5 - Option for giving the length vs needing to calculate it] 
::: [6 - var containing Width to pull from] 
::: [7 - var containing Blanks to pull from] 
::: [8 - Option to add CRLF at the end of string]
::: [9 - Border Option]
::: [10 - Option to justify text 1/left, 2/right, 3/split - right text supplied Arg4, 4/centered, 5/split - left text supplied Arg5] 
::: [11 - Left portion of Text if Justy is split [spaces in between text]]

出于速度原因,最好将说明移到标签之前。


此类问题的另一个示例,但这与行结尾相关

@echo off

goto :LineBuilder
REM 12345678
:LineBuilder [1 - main var type 1] [2 - main var type 2] [3 - subvar type] [4 - string to store in 1.3] [5 - Option for giving the length vs needing to calculate it] [6 - var containing Width to pull from] [7 - var containing Blanks to pull from] [8 - Option to add CRLF at the end of string] [9 - Border Option] [10 - Option to justify text 1/left, 2/right, 3/split - right text supplied Arg4, 4/centered, 5/split - left text supplied Arg5] [11 - Left portion of Text if Justy is split [spaces in between text]]

使用 LF 行结尾保存此内容(使用 vscode,您可以在 CRLF 和 LF 之间切换)

然后你会得到:

'etween' is not recognized as an internal or external command, operable program or batch file.

“未知”命令仅取决于 REM 行中的字符数。

如何解决?

使用 CRLF 行结尾保存文件或使用

more defectBatch.bat > workingBatch.bat

关于batch-file - 在批处理脚本中,标签和标签后面出现的文本是否有记录的长度限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77185505/

相关文章:

java - 在启动前确定最佳 Java 运行时参数?

java - 如何为 Eclipse 创建新的键绑定(bind)

certificate - UWP 应用 - 更新证书后包系列名称无效

xpath - 在Xquery中使用fn:string-length时出错

c++ - 为什么 strlen() 比手动循环检查空终止字符快大约 20 倍?

windows - 使用批处理脚本进行字符串解析

batch-file - 用于安装 MSI 的批处理脚本

powershell - 从批处理文件调用时,Send-MailMessage cmdlet捕获错误

windows-runtime - 如何在 Windows 10 周年更新上获取唯一的设备 ID

java.util.UUID.randomUUID().toString() 长度