Batch : label length
I was wondering about the maximum length of a label in a batch file.
I found this Microsoft article s开发者_如何学Pythontating:
MS-DOS recognizes only the first eight characters of a batch file label; subsequent characters are ignored.
They also provide an example :
@echo off
goto latestch
:latestchanges
echo two
:latestch
echo three
which is supposed to output
two
three
instead of
three
But on my system, I get
three
I tried on Windows 7 (6.1.7600) and WindowsXP (5.1.2600), and get the same result on both of them.
It looks to me there is no eight characters limitation!
Am I missing something?The limits are 2047 and 8192, depending on your OS. See this KB article.
The example is true for MS-DOS
not cmd.exe
. The version of your cmd.exe
is higher than MS-DOS
. Feel free to use any length of label.
According to that article, this limitation is valid for :
Microsoft MS-DOS 4.01 Standard Edition Microsoft MS-DOS 5.0 Standard Edition Microsoft MS-DOS 5.0a Microsoft MS-DOS 6.0 Standard Edition Microsoft MS-DOS 6.2 Standard Edition Microsoft MS-DOS 6.21 Standard Edition Microsoft MS-DOS 6.22 Standard Edition
I'm pretty sure the 8 character limitation went away when Windows moved away from the MS-DOS platform after Windows 98. All Microsoft OSes starting with Windows 2000 no longer have the limitation. The command window that we see today in Windows 7 and others is an application that runs on top of Windows, rather than the older implementation where the command window accessed the MS-DOS layer running beneath Windows.
Windows cmd.exe supports label lengths up to 128 characters long (including the leading colon). Any characters after 128 are simply ignored.
So a label length 500 will match a label length 300 if the first 128 characters of both labels is the same.
Here is a batch script that demonstrates the behavior:
@echo off
call :xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 125
call :xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 126
call :xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxx
echo %1 finds 128
exit /b
:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo %1 finds 129
exit /b
:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo %1 finds 130
exit /b
-- OUTPUT --
125 finds 125
126 finds 126
127 finds 127
128 finds 128
129 finds 128
130 finds 128
Windows 7 CMD and BAT batch commands that use the GOTO :LABEL
are not limited to 8 characters following the ":" character as initially noted by the original poster when they are executed directly or CALLed from another batch file.
i.e.,
@echo off
SET VARIABLE=2
if %VARIABLE%.==. GOTO :LABELNUMBERZERO
if %VARIABLE%.==1. GOTO :LABELNUMBERONE
if %VARIABLE%.==2. GOTO :LABELNUMBERTWO
if %VARIABLE%.==3. GOTO :LABELNU
if %VARIABLE%.==4. GOTO :LABELN
GOTO :ENDTHISLONGTHING
:LABELNUMBERZERO
echo your variable was " "
GOTO :ENDTHISLONGTHING
:LABELNUMBERONE
echo your variable was "1"
GOTO :ENDTHISLONGTHING
:LABELNUMBERTWO
echo your variable was "2"
:ENDTHISLONGTHING
:LABELNU
echo your variable was "3"
:ENDTHISLONGTHING
:LABELN
echo your variable was "4"
:ENDTHISLONGTHING
The result of this is: your variable was "2"
If I set
the VARIABLE=4
the result is:
your variable was "4"
So DOS now sees even similarly named (beginning characters) as unique labels even if the same contents of a shorter label exist in the batch file beforehand.
精彩评论