简体   繁体   English

Windows 批处理文件 - 系统找不到指定的批处理标签

[英]Windows batch file - The system cannot find the batch label specified

The Problem问题

I'm having a problem with a Windows batch file and labels.我的 Windows 批处理文件和标签有问题。 I keep getting this error:我不断收到此错误:

The system cannot find the batch label specified系统找不到指定的批次标签

What I've tried我试过的

  • Two computers;两台电脑; a WindowsXP and a 2003 Server.一个 WindowsXP 和一个 2003 服务器。
  • Made sure it was encoded as ASCII确保它被编码为 ASCII
  • Editted the hex code for the line continuation characters.编辑了行继续符的十六进制代码。 Tried replacing all with CR , LF, and CRLF in turn.尝试用 CR 、 LF 和 CRLF 依次替换 all 。 All combinations give me the same error.所有组合都给我同样的错误。
  • Tried inserting extra characters before the label to make the label past 512 characters.尝试在标签前插入额外的字符以使标签超过 512 个字符。

Here is the code:这是代码:

cls
@echo off
SET zip=7za a dependencies.7z
call:dozip "c:\temp\dir.txt"

pause
goto exit

:dozip
  echo Testing 1.2.3...
  %zip% %1
goto:eof
:exit

Here's the hex with CRLF (0d 0a).这是带有 CRLF (0d 0a) 的十六进制。

63 6c 73 0d 0a 53 45 54 20 7a 69 70 3d 37 7a 61 20 61 20 64 65 70 65 6e 64 65 6e 63 69 65 73 2e 37 7a 0d 0a 63 61 6c 6c 3a 64 6f 7a 69 70 20 22 63 3a 5c 74 65 6d 70 5c 64 69 72 2e 74 78 74 22 0d 0a 0d 0a 70 61 75 73 65 0d 0a 67 6f 74 6f 20 65 78 69 74 0d 0a 0d 0a 3a 64 6f 7a 69 70 0d 0a 20 20 65 63 68 6f 20 54 65 73 74 69 6e 67 20 31 2e 32 2e 33 2e 2e 2e 0d 0a 20 20 25 7a 69 70 25 20 25 31 0d 0a 67 6f 74 6f 3a 65 6f 66 0d 0a 3a 65 78 69 74

Here's the console's output (when I remove @echo off):这是控制台的输出(当我删除 @echo off 时):

C:\>SET zip=7za a dependencies.7z

C:\>call:dozip "c:\temp\dir.txt"

C:\>echo Testing 1.2.3...
Testing 1.2.3...

C:\>7za a dependencies.7z "c:\temp\dir.txt"
The system cannot find the batch label specified - dozip

C:\>pause
Press any key to continue . . .

It never actually creates the 7zip file, so I think I can assume that its crashing on this line;它实际上从未创建 7zip 文件,所以我想我可以假设它在这一行崩溃了;

7za a dependencies.7z "c:\temp\dir.txt"

If I run that line by itself from a command prompt, it works fine and creates the dependencies.7z, so I don't think its necessarily a problem with 7za.exe.如果我从命令提示符单独运行该行,它可以正常工作并创建dependencies.7z,所以我认为它不一定是 7za.exe 的问题。

I've already read this stackoverflow question: stackoverflow.com/questions/232651/why-the-system-cannot-find-the-batch-label-specified-is-thrown-even-if-label-ex我已经阅读了这个 stackoverflow 问题:stackoverflow.com/questions/232651/why-the-system-cannot-find-the-batch-label-specified-is-thrown-even-if-label-ex

and the link from that post;以及该帖子的链接; help.wugnet.com/windows/system-find-batch-label-ftopict615555.html help.wugnet.com/windows/system-find-batch-label-ftopict615555.html

The Answer答案

So, I found the problem guys.所以,我发现了问题的家伙。

I was using a technique I commonly use that I could only really describe as "proxy" batch files.我正在使用一种我常用的技术,我只能将其描述为“代理”批处理文件。 I have a folder called c:\\scripts, and I put several bat files in there to target commonly used exes.我有一个名为 c:\\scripts 的文件夹,我在里面放了几个 bat 文件来定位常用的 exe。 This saves my PATH variable from becoming absolutely massive with all of my command line tools.这使我的 PATH 变量免于使用我的所有命令行工具变得绝对庞大。 This way I only need to add c:\\scripts to my PATH, and create a proxy batch file when I need something.这样我只需要将 c:\\scripts 添加到我的 PATH 中,并在需要时创建一个代理批处理文件。

I had 7za.bat in c:\\scripts, containing only this;我在 c:\\scripts 中有 7za.bat,只包含这个;

@echo off
"C:\Program Files\7-zip\7za.exe" %*

I changed my script to this;我把我的脚本改成了这个;

SET zip="c:\program files\7-zip\7za.exe" a dependencies.7z

instead of this;而不是这个;

SET zip=7za a dependencies.7z

and it worked flawlessly.它完美无缺。

The moral of the story...这个故事的主旨...

Avoid calling other batch files from within a batch file.避免从批处理文件中调用其他批处理文件。 If you do, you will need to prefix them with " call ".如果这样做,则需要在它们前面加上“ call ”。

I would point out that the "Testing 1.2.3..." and "Press any key to continue . . ."我要指出的是“测试 1.2.3...”和“按任意键继续...” lines indicate that execution has successfully gone to the :dozip label and then successfully returned to the caller.行表示执行已成功转到 :dozip 标签,然后成功返回给调用者。

Is the "7za" executable actually a batch file? “7za”可执行文件实际上是批处理文件吗? If I modify my test script to have the helper be a batch file, I get the same error.如果我修改我的测试脚本以使帮助程序成为批处理文件,则会出现相同的错误。 The fix is to do 'call %zip% %1'解决方法是执行“呼叫 %zip% %1”

the moral of the story: when calling external programs/batch files in a batch file, use call故事的寓意:在批处理文件中调用外部程序/批处理文件时,使用 call

call foo.bat

and/or和/或

call %foo%

(Calling one batch from another has been done since the days of DOS, just remember to call) (从 DOS 时代就已经完成了从另一个批次调用,只要记得调用)

One possibility, although it seems unlikely, is that command extensions aren't enabled, or up-to-date, and this is interfering with call/goto/label behaviour.一种可能性(尽管看起来不太可能)是命令扩展未启用或未更新,这会干扰 call/goto/label 行为。

Try:尝试:

echo [%cmdextversion%]

and if it's less than [2] (or empty -- [] ) then check to see if cmd.exe is being invoked with /e:off , or just run如果它小于[2] (或空 - [] ),则检查是否正在使用/e:off调用cmd.exe ,或者直接运行

cmd /e:on

in the console window where you will run this batch file.在您将运行此批处理文件的控制台窗口中。

The "The system cannot find the batch label specified" error message brought me here (10 years later!) and the problem turned out to be the CRLF was incorrect. “系统找不到指定的批次标签”错误消息将我带到这里(10 年后!),结果证明 CRLF 不正确。

The cause, in our case, was a Git Repository that didn't recognize BAT files as being text files which needed to be CRLF on a Windows 7 machine.在我们的案例中,原因是 Git 存储库无法将 BAT 文件识别为需要在 Windows 7 机器上进行 CRLF 的文本文件。

Our solution was to create a .gitattributes file containing the line:我们的解决方案是创建一个包含以下行的 .gitattributes 文件:

*.bat text eol=crlf

Then deleting the BAT files and checking out from the repository re-wrote our BAT files with the correct line endings.然后删除 BAT 文件并从存储库中检出以正确的行结尾重新编写了我们的 BAT 文件。 The BAT labels are now working again. BAT 标签现在又开始工作了。

In my case the reasons of "The system cannot find the batch label specified" were:在我的情况下,“系统找不到指定的批次标签”的原因是:

  1. No CALL before jar- and unzip-commands在 jar- 和 unzip-commands 之前没有 CALL
  2. No ENDLOCAL for every SETLOCAL DisableDelayedExpansion每个 SETLOCAL DisableDelayedExpansion 都没有 ENDLOCAL
  3. Some other batch-magic, which I didn't completely understood near SETLOCAL DisableDelayedExpansion, it was important to place SET param=1 after SETLOCAL DisableDelayedExpansion or something like that其他一些批处理魔术,我在 SETLOCAL DisableDelayedExpansion 附近没有完全理解,重要的是在 SETLOCAL DisableDelayedExpansion 之后放置 SET param=1 或类似的东西

Are you using Windows NT 4/Windows 2000 ?您使用的是Windows NT 4/Windows 2000吗? Only there can you use CALL to call subroutines within the same batch file.只有在那里你才能使用 CALL 来调用同一个批处理文件中的子程序。

Looking closely at your hex, it does not actually have all CRLF ( 0d 0a ).仔细观察你的十六进制,它实际上并没有所有的 CRLF ( 0d 0a )。 Several lines end in LF-only ( 0a without a preceding 0d ).几行以 LF-only 结尾( 0a没有前面的0d )。

Check in your hex editor to make sure every 0a is preceded by 0d (exactly one).检查您的十六进制编辑器以确保每个0a前面都有0d (正好是 1)。

Or just cut and paste your file into a blank Notepad document and re-save it.或者只是将您的文件剪切并粘贴到空白的记事本文档中,然后重新保存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 windows批处理文件系统找不到指定的批处理标签-退出 - windows batch file The system cannot find the batch label specified-exit 系统找不到指定的批次标签 - The system cannot find the batch label specified 系统找不到指定的路径-批处理文件 - The system cannot find the path specified - Batch file “ yarn resourcemanager”产生“系统找不到指定的批次标签-resourcemanager” - “yarn resourcemanager” yields “The system cannot find the batch label specified - resourcemanager” Windows批处理文件中来自远程存储库的svn head修订-系统找不到指定的文件 - svn head revision from a remote repository in windows batch file - the system cannot find the file specified Windows 批处理脚本“系统找不到指定的文件。” 文件名中有感叹号? - Windows batch script "The system cannot find the file specified." with exclamation in filenames? 批处理文件中出现意外的“系统无法找到指定的驱动器。” - Unexpected “The system cannot find the drive specified.” in batch file 即使label存在,为什么会抛出“系统找不到指定的批次label”? - Why "The system cannot find the batch label specified" is thrown even if label exists? 批处理-系统找不到指定的文件 - Batch - system can not find the file specified 运行Babun,出现错误“系统找不到指定的批次标签-CHECKTARGET” - Running Babun, Getting error “The system cannot find the batch label specified - CHECKTARGET”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM