batch-filewindows-10string-length

In a batch script, is there a documented length limit for Labels and the text which appears _after_ the Label?


Consider the following Label with text added after to clarify what the arguments are for in a batch script (anything after a space is ignored . . .):

: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]]

Referring to this SO Article, I know the maximum length of the Label itself is 128 characters. Is it also documented how long the length of the text after the label can be? If not officially documented, or if this specific question is not already answered, I may have the answer, but want to confirm it's not listed somewhere else.

Not including the colon (:), the entire line is 511 characters. The length of the string afterward, including all spaces (and the space separating the arg clarifications from the Label itself) is 500 characters long. How I ended up stumbling upon this was getting an error which took me a while to pinpoint! It appeared the CMD Line Interpreter gave up attempting to store the Label's string data in one single string anymore, and attempted to parse the final ] as a Command! (If you add a REM just after the first place, the error changes to ext]] - an increase of exactly 4 characters.

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

Experiencing this in Windows 10.

Update

Based on jeb's answer below, and the question in the comments, I can attest the file is saved as CR LF line endings (Verified as shown in NotePad++, Encoding's UTF-8 (does this make a difference?). . .

Minimal example showing where it's in use:

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_-_-_-_-_->>>

If I change the text after the label to be the following, the error goes away:

 :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]]

Solution

  • A minimal example for long label lines:

    @echo off
    goto :myLabel
    
    :myLabel xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx12345678
    

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

    In the label line, the text "1234578" starts after 512 leading characters (the colon label name and many x's).

    Probably the label parser has a buffer for 512 bytes (+2 for CRLF), when there is no CRLF found in that buffer, it simply stops searching for CRLF and just use the following characters.

    The rule is: If the line of a label is longer than 514 bytes, the parser simply assumes the next command at position 514.

    You should solve your problem by just move your description to the next line, btw. that is much more readable than one ultra long line.

    :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]]
    

    For speed reasons, it's better to move the description before the label.


    Another expample for this kind of problem, but this is related to line endings

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

    Save this with LF line endings (With vscode you can switch between CRLF and LF)

    Then you get:

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

    The "unknown" command only depends on the count of characters in the REM line.

    How to solve?

    Save the file with CRLF line endings or use

    more defectBatch.bat > workingBatch.bat