83

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?

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Slimak
  • 1,396
  • 1
  • 8
  • 17

10 Answers10

96

Actually, you need 2 conditions for this to happen:

  • the batch file must not use CRLF line endings
  • the label you jump to must span a block boundary (as opposed to and :end label wich is just a shortcut to the end of your script)

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 Studio vcvarsall.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"
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 3
    Had this issue with the ant batch file shipped with eclipse and being called from MSVC - changing line endings all fixed, thankyou. – Greg Domjan Jun 03 '09 at 02:08
  • Found this issue with batch scripts shipped with Groovy - all the scripts contained LF only, I had to manually replace it with CRLF. – Neel Jun 06 '15 at 22:36
  • 3
    This probably another sick joke from Microsoft... took me forever to figure this out.. of course Notepad++ doesn't care to line endings unless you go and tell it to convert to the other... mine as set to LF only.. Thanks. – AlexD Oct 28 '16 at 20:37
  • Nathan's answer below is also a likely culprit. – Stanley.Goldman Jul 12 '17 at 16:33
  • https://www.jetbrains.com/help/idea/configuring-line-endings-and-line-separators.html#d76642e48 this link may be useful for IntelliJ IDEA user who wants to know, how to change line separators for a file. – naXa stands with Ukraine Apr 11 '18 at 15:02
  • Interesting issue/concept. I wonder if this problem is restricted to XP. I cannot reproduce the behavior on Win 10. – dbenham Feb 03 '19 at 18:11
  • @dbenham 10+ years later, it is possible the issue does not apply anymore. After all, even Notepad itself supports LF now, not just CRLF: https://blogs.msdn.microsoft.com/commandline/2018/05/08/extended-eol-in-notepad/ – VonC Feb 03 '19 at 20:23
  • Actually I was finally able to reproduce the problem on Win 10. We did some [experiments at DosTips](https://www.dostips.com/forum/viewtopic.php?f=3&t=8988), and it turns out the 512 boundary when jumping forward is relative to the current file position upon CALL or GOTO execution, not the start of file. When jumping backward, it is relative to the start of file. – dbenham Feb 11 '19 at 02:29
  • FWIW, @Nathan's answer applies as well to Windows 10, where I just encountered it when I launched a script that I created two years ago, and discovered that its line endings got converted sometime between then and now. I quickly verified and fixed it with the help of my trusty text editor, UltraEdit. – David A. Gray Aug 08 '19 at 06:01
  • I just hopped over to https://web.archive.org/web/20160305063042/help.wugnet.com/windows/system-find-batch-label-ftopict615555.html and read the article. This must be an edge case of some sort, because 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. – David A. Gray Aug 08 '19 at 06:07
  • @DavidA.Gray Thank you. I have included your comment in the answer for more visibility. – VonC Aug 08 '19 at 06:16
  • this is happening to me even though I'm using CRLF. Any ideas? – Mark Deven Oct 12 '19 at 22:16
  • @MarkDeven Eleven years later, not on the top of my HEAD. You would have to check you OS, your text encoding, your script extension (`.cmd` or `.bat`), ... and ask a separate question. – VonC Oct 12 '19 at 22:27
  • 2
    @MarkDeven - At DosTips I have [demonstrated how a valid `:label` can be missed](https://www.dostips.com/forum/viewtopic.php?f=3&t=8988#p58890) even when using CRLF line endings, but I doubt that is the source of your problem. Later in the same DosTips thread I list the [rules as I currently understand them for how batch scans for a label](https://www.dostips.com/forum/viewtopic.php?f=3&t=8988&start=15#p58925). I agree with VonC that you might want to ask a separate SO question, or you might consider contributing your problem to the DosTips discussion. – dbenham Nov 20 '19 at 18:28
  • 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 Studio vcvarsall.bat file on a system without VS installed. – kinar Feb 17 '20 at 14:24
  • 1
    @kinar Thank you for this feedback my this old 2008 answer. I have included your comment in the answer for more visibility. – VonC Feb 17 '20 at 14:30
  • @kinar I don't believe that the error occours when using `CALL not-existant-batch`. I suppose it's only a simple undefined label somewhere or perhaps [the case I described](https://stackoverflow.com/a/60265222/463115) – jeb Feb 17 '20 at 15:04
  • 1
    @jeb Good point. I have included a link to your answer in mine, for more visibility. – VonC Feb 17 '20 at 15:18
  • @jeb you're right. It wasn't the call to the batch file itself. It was actually exactly what the error described. There was a "goto label" buried deep in a messy block of if/else conditionals that it was hitting. oops. – kinar Feb 17 '20 at 15:35
  • repro-ed today on windows 10. i had cloned a git repo forcing \n, i suppose, including this file. – Luke Jul 14 '21 at 04:24
  • 2
    @Luke Good point (impressed that this answer is still relevant 13 years later). That is why I always have in my Git repositories a `.gitattributes` file with the directive `*.bat text eol=crlf`. – VonC Jul 14 '21 at 08:42
  • I have uploaded a few bat files to my GIT repository, when downloading the bat file from git website and executing it I get the error `The system cannot find the batch label specified`. Added gitattributes but still its the same. – Adhip Rebello Jun 23 '23 at 08:20
  • @AdhipRebello I have edited the answer to add the missing step: `git add ---renormalize .`, in order to apply your new `.gitattributes` directives. Then push, and try to clone again. Make sure that `git config --global core.autocrlf` is set to `false`. – VonC Jun 23 '23 at 10:26
54

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.

Nathan
  • 8,093
  • 8
  • 50
  • 76
Marshal
  • 4,452
  • 1
  • 23
  • 15
  • I would like to point out that this is an additional reason. I had this issue when calling and external program and this suggestion solved the problem. – Joel Slowik Oct 28 '14 at 14:59
  • Found this issue with batch scripts shipped with Groovy - not sure why they do not have exe's instead for windows! – Neel Jun 06 '15 at 22:44
  • 4
    In Windows, you might be running a `.bat` and not realize it! I just learned that running `mvn` actually executes a batch file, `mvn.cmd`, and lost an hour trying to understand why my scripts were failing with this error anytime I ran `mvn`. – jordanpg Aug 18 '15 at 14:24
  • @jordanpg yeah, you are right! I had the same issue when I tried to create bat for clean install and deploying. Marshal thanks to you too. – Ashok M A Feb 20 '17 at 12:00
  • 4
    Strictly speaking, what @Nathan says about CALLing external programs from scripts applies only to other batch scripts (.BAT and .CMD files). I haven't tried CALLing an executable program (.EXE file), though I suspect the CALL is harmless. With regard to EXE files, there is also START, which is useful when the program in question creates a new process, for instance, when the program in question is a graphical Windows program. – David A. Gray Aug 08 '19 at 05:56
  • @jordanpg Glad I found this thread because funnily enough, I also had this problem because I was running Maven on Windows. This is also an issue with `conda` for example. – phetdam Feb 07 '23 at 20:40
12

If batch file has unix line endings (line separators) this can sometimes happen.

Just unix2dos it and problem should be solved.

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
Slimak
  • 1,396
  • 1
  • 8
  • 17
11

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.

Masoud Kermani
  • 111
  • 1
  • 2
  • 2
    Nope, the `\n` is not treated as part of the label, and adding a space after the label does not help. The problem truly is as [VonC reported](https://stackoverflow.com/a/232674/1012053), and as I demonstrate at https://www.dostips.com/forum/viewtopic.php?f=3&t=8988#p58888 – dbenham Feb 11 '19 at 02:38
5

You should also make sure that when calling other scripts you use CALL, instead of calling them in the caller's environment.

3

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".

Greg
  • 1,181
  • 13
  • 13
  • Regarding VonC's comment, it is logically correct, though worded in a confusing manner. He says (paraphrasing), for the error to occur, these two conditions must be true. ERROR == ( A && B ), therefore, if, like most people you do NOT want an error, then you invert what he said, ! ERROR == ( ! A || ! B ), so to avoid the error, the inversion of either one condition, the other, or both must be true. So to avoid error, use CRLF or avoid spanning a block boundary. For simpler batch files, CR LF will probably work. –  May 28 '17 at 01:56
3

There are multiple possible ways to get the error.

  1. Described by VonC - Wrong line endings, LF instead of CR/LF

  2. Obscure long lines (if that happens accidential, your code is incredible worse)

  3. 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.
    But this can be (mis)-used to directly call labels in another batch file

This is the described behaviour of the answer of Marshal

jeb
  • 78,592
  • 17
  • 171
  • 225
2

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

Stefan Michev
  • 4,795
  • 3
  • 35
  • 30
0

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).

0

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.

Cristian F.
  • 328
  • 2
  • 12