Why “The system cannot find the batch label specified” is thrown even if label exists?

后端 未结 9 1198
后悔当初
后悔当初 2020-11-30 02:48

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

相关标签:
9条回答
  • 2020-11-30 03:24

    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 讨论(0)
  • 2020-11-30 03:28

    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.

    0 讨论(0)
  • 2020-11-30 03:37

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

    Just unix2dos it and problem should be solved.

    0 讨论(0)
提交回复
热议问题