Reputation: 1446
While running a batch file in Windows XP I have found randomly occurring error message:
The system cannot find the batch label specified name_of_label
Of course label existed. What causes this error?
Upvotes: 87
Views: 94960
Reputation: 1324347
Actually, you need 2 conditions for this to happen:
See. The system cannot find the batch label specified (by and Batch-as-batch-can!
David A. Gray mentions in the comments seeing (on Windows 10) what Marshal's answer showed in 2014 (presumably on Windows 7 or 8): a script/batch program (.bat
or .cmd
) executed without CALL
would trigger an eol conversion.
I've written hundreds of batch scripts over the last 35 years, and the only time I've ever had an issue with labels not being found was when the file's line breaks got converted from Windows (CR/LF), which works, to Unix (LF), which doesn't.
Feb. 2020, kinar adds in the comments:
Just encountered this issue on a Win7 machine.
Turns out this error can also be generated when trying to CALL another.bat
file if that file doesn't exist on the system.
In my case, I was trying to call the Visual Studiovcvarsall.bat
file on a system without VS installed.
See jeb's answer for more: it was a case of an undefined label.
Note: in a Git repository, I would recommend a .gitattributes
file with the directive:
*.bat text eol=crlf
If you change it afterward, you need, with Git 2.16+ (Q1 2018):
cd /path/to/repo
git add --renormalize .
git commit -m "apply new .gitattributes"
Upvotes: 101
Reputation: 480
I had the error :
The system cannot find the batch label specified -
and I found that on a line I used
goto : eof
instead
goto :eof
So check for the same issue of using labels, if the solutions from above didn't worked out.
Upvotes: 2
Reputation: 82307
There are multiple possible ways to get the error.
Described by VonC - Wrong line endings, LF instead of CR/LF
Obscure long lines (if that happens accidential, your code is incredible worse)
Direct start another batch after calling a function.
Sample:
@echo off
call :func
echo back from second
exit /b
:func
second.bat
echo NEVER COME BACK HERE
This unexpectedly tries to goto
to the label :func
in second.bat.This is the described behaviour of the answer of Marshal
Upvotes: 3
Reputation: 131
Little different use case ...
I was calling a bat script during packer build of Windows Server 2012 Server, using the shell provisioner (OpenSSH). Now, the script was working fine through cmd in the provisioned Virtual Machine (put breakpoint in packer build to stop and confirmed this) ... but, it was failing with these call labels not found issues.
The Line Endings were fine, CRLF (confirmed in Notepadd++). The script was working fine through command line as well. What more, sometimes, it just use to run fine and sometime fail, but once failed for some label, the failure was consistent.
Initially, I just started removing the subroutines altogether by expanding the call itself and putting subroutine code inline. I did this for all instances where there was only one call (no code duplication).
But, yeah, i did stumble upon one sub which was called from 3,4 places. After trying everything, this is what worked for me
Adding 8-10 REM statements just above the subroutine. Yes, I am not kidding !!
PS : The script is very very old, but management needed me to make that work through packer (we have a Day-2 plan of this to replace it with Ansible/Chef).
Upvotes: 0
Reputation: 1446
If batch file has unix line endings (line separators) this can sometimes happen.
Just unix2dos it and problem should be solved.
Upvotes: 13
Reputation: 4860
I have got the same issue before. However, the root cause was not CRLF at all. It was because in the script I executed an external program such as Ant, but did not put a CALL
before Ant. So, make sure you CALL
every external program used in your batch script.
Upvotes: 57
Reputation: 1251
I encountered a similar issue just now with a .cmd file and Windows 8. The solution was to change all line endings to CR+LF DOS style. The issue was confusing because the batch file mostly worked and rearranging lines changed the effect.
The .cmd file looked like:
call:function_A "..\..\folderA\"
call:function_B "..\..\folderB\"
call:function_C "..\..\folderC\"
call:function_D "..\..\folderD\"
goto:eof
:function_A
rem do stuff
goto:eof
...etc...
Function C would cause error "The system cannot find the batch label specified". Strangely it could go away by rearranging the calls. Changing line endings from 0x0A to 0x0D0A seems to have fixed it.
Perhaps VonC meant "the batch file must use CRLF line endings".
Upvotes: 3
Reputation: 5093
i had this issue after copying a start command from word and paste it into the command window. There was a option with "-" on front, and thought the looks the same as the DOS "-" it wasn't :) After typing the "-" by myself the issue was solved and the batch worked ... a hard to find issue ....
Upvotes: 2
Reputation: 121
Here is the issue and how to fix it. The issue is a bug or a feature in DOS batch cmd program. First the clear problem statement. If you have a DOS batch file with target labels like, ":dothis", and at the end of the label you do not have space then the batch file will not work if the line ending are UNIX line endings. This means you have to run unix2dos on the file before you can use it.
The root cause is the DOS command line processor, (shell program), takes the UNIX end-of-line character as part of the label. Since the go to part never uses this as the label, it is never found since such a label truly does not exist. The solution is to put an extra space at the end of each target label, or even better every line. Now UNIX end of lines do not come to play since the space acts as the separator and it all works.
Upvotes: 12
Reputation:
You should also make sure that when calling other scripts you use CALL, instead of calling them in the caller's environment.
Upvotes: 5