简体   繁体   English

如何使用START命令启动批处理文件的返回/退出代码?

[英]How to get return/exit code of a batch file started with START command?

I am trying to run a batch file with start /high and still get the return/exit code, ie %ERRORLEVEL% . 我正在尝试使用start /high运行批处理文件,但仍然获得返回/退出代码,即%ERRORLEVEL% The problem seems to be that command START does not return the exit code that the batch file returns. 问题似乎是命令START不返回批处理文件返回的退出代码。

We have a simple batch file for testing named BatFileThatReturnsOne.bat . 我们有一个简单的批处理文件,用于测试名为BatFileThatReturnsOne.bat测试。

The contents of BatFileThatReturnsOne.bat are BatFileThatReturnsOne.bat的内容是

EXIT /B 1

We are calling this as such: 我们这样称呼:

start /high /wait BatFileThatReturnsOne.bat

But no matter what the batch file returns, the execution of start never has a %ERRORLEVEL% of anything other than 0 (zero). 但无论批处理文件返回什么, start的执行从不具有除0(零)以外的任何值的%ERRORLEVEL%

This is all actually called by cfn-init in CloudFormation, but that is probably not relevant because we can reproduce it from a command line window. 这实际上是由CloudFormation中的cfn-init调用的,但这可能不相关,因为我们可以从命令行窗口重现它。

The actual call is: 实际的电话是:

cmd.exe /C start /high /wait BatFileThatReturnsOne.bat

How do I get start to set the %ERRORLEVEL% to something other than 0 (zero)? 如何start%ERRORLEVEL%设置为0(零)以外的值?

directly from a cmd window or a batch file you can use 直接来自cmd窗口或您可以使用的批处理文件

start /high /wait cmd /c BatFileThatReturnsOne.bat

but if you need to start the cmd instance to execute the start command that launchs the batch file then you can use 但是如果您需要启动cmd实例来执行启动批处理文件的启动命令,那么您可以使用

cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"

Just change EXIT /B 1 by EXIT 1 . 只需通过EXIT 1更改EXIT /B 1 EXIT 1

As explained in the Table 4 given in this answer about START /WAIT bat command: 本答案中给出的关于START /WAIT bat命令的表4中所述:

When the started Batch file ends, set ERRORLEVEL = value from 'EXIT number' commmand.

暂无
暂无

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

相关问题 批处理文件中的“EXIT”不能覆盖以前命令的返回代码 - “EXIT” in batch file cannot overwrite return code of previous commands 为什么通过批处理文件使用命令start启动的程序在通过Windows资源管理器运行时无法运行? - Why does a program started by a batch file using command start not run while it runs via Windows Explorer? 如何编辑BATCH启动命令以启动上述文件夹中的文件 - How to edit BATCH start command to start a file in folder above Windows批处理文件-程序关闭后如何在批处理文件中未启动的命令执行 - Windows batch file - how to execute command after program close that's not started in the batch file 如何从批处理文件中获取刚刚启动的进程的PID? - How to get PID of process just started from within a batch file? 如何在 Windows 批处理文件中打印上次执行的应用程序的退出代码? - How to print the exit code of the last executed application in a Windows batch file? 批处理文件“启动”命令和参数 - Batch file “start” command and parameters 使用“cmd”和“start”命令启动应用程序的退出代码 - Getting the exit code of an application started with the “cmd” and “start” commands 无法启动 Eclipse - Java 已启动但返回退出代码 = 13 - Can't start Eclipse - Java was started but returned exit code=13 如何在Windows批处理文件中获取Java程序的退出状态 - How to get the exit status of a Java program in Windows batch file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM