启动/等待/ B时出现错误不退出程序 [英] Start /wait /b not exiting program when there is an error

查看:145
本文介绍了启动/等待/ B时出现错误不退出程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将运行其他几个文件的批处理文件(可以称之为程序文件)如.bat,.exe文件,的.py,等等...

 如果不存在JobStreamUnitTest_CreateTextPython_4-27-2015.txt(
    启动/等待/ B C:\\用户\\ blee2 \\文档\\的UnitTest \\ CreateTextFile.py ||退出%ERRORLEVEL%
    复制/ Y NUL JobStreamUnitTest_CreateTextPython_4-27-2015.txt
)如果不存在JobStreamUnitTest_CreateTextBatch_4-27-2015.txt(
    启动/等待/ B C:\\用户\\ blee2 \\文档\\的UnitTest \\ CreateNewFile.bat ||退出%ERRORLEVEL%
    复制/ Y NUL JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)如果不存在JobStreamUnitTest_CreateTextConsole_4-27-2015.txt(
    启动/等待/ B C:\\用户\\ blee2 \\文档\\的UnitTest \\ TestConsole.exe苹果||退出%ERRORLEVEL%
    复制/ Y NUL JobStreamUnitTest_CreateTextConsole_4-27-2015.txt
)如果不存在JobStreamUnitTest_HelloWorld_4-27-2015.txt(
    启动/等待/ B C:\\用户\\ blee2 \\文档\\的UnitTest \\ HelloWorld.bat ||退出%ERRORLEVEL%
    复制/ Y NUL JobStreamUnitTest_HelloWorld_4-27-2015.txt

所以基本上,该批处理文件将检查以下文件需要根据每个程序文件的虚拟文件关联的存在来运行。
这将$ P $的成功运行运行,如果我们要运行该批处理文件第二次pvent我们。

如果有任何的程序文件没有错误,则code将正常工作。

如果文件/文件路径是不正确的退出错误才起作用。
我现在面临的问题是,既然开始/等待/ B将一直执行不管,如果我的程序文件的一个有一个错误。因此,退出%ERRORLEVEL%的人会无法运行。

如何允许如果一个程序文件被破坏的批处理文件来检测错误?
我想退出/终端的批处理文件,如果程序文件的一个不工作。有什么想法吗?

PS。需要/等待,因为开始应以连续的顺序运行。结果
需要/ B,否则程序将运行一个.BAT后停止; / B让我们运行在同一个cmd窗口的批处理文件。

鸭preciate任何帮助,谢谢

编辑:
如果我这样做了code会工作。但我希望能有一个一致性在我的批处理文件格式,因为由C#生成批处理文件与.xml文件解析。

 如果不存在JobStreamUnitTest_CreateTextPython_4-27-2015.txt(
    C:\\用户\\ blee2 \\文档\\的UnitTest \\ CreateTextFile.py ||退出%ERRORLEVEL%
    复制/ Y NUL JobStreamUnitTest_CreateTextPython_4-27-2015.txt
)如果不存在JobStreamUnitTest_CreateTextBatch_4-27-2015.txt(
    启动/等待/ B C:\\用户\\ blee2 \\文档\\的UnitTest \\ CreateNewFile.bat ||退出%ERRORLEVEL%
    复制/ Y NUL JobStreamUnitTest_CreateTextBatch_4-27-2015.txt


解决方案

我已经发现了一些问题,启动/等待/ B any_program ||退出%ERRORLEVEL%


  • #1 - %ERRORLEVEL%变量将在解析时被扩展。因此,你的脚本不返回正确的退出code。 见EnableDelayedExpansion

  • 2 - || 有条件的命令执行:可惜我不能正确地将其记录下来,但我的这一切努力的失败有关到启动命令...

恕我直言接下来的code段(唯一的例子),可以按预期工作:

 如果不存在JobStreamUnitTest_CreateTextBatch_4-27-2015.txt(
    启动/ B / WAIT C:\\用户\\ blee2 \\文档\\的UnitTest \\ CreateNewFile.bat
    SETLOCAL enabledelayedexpansion
    如果!错误级别! NEQ 0退出!错误级别!
    ENDLOCAL
    复制/ Y NUL JobStreamUnitTest_CreateTextBatch_4-27-2015.txt


  • #3 - 在实施情况的错误启动命令

启动/等待/ B 不起作用(在/等待参数被忽略):

  ==>启动/等待/ B WMIC OS GET标题和放大器;回声XXX
XXX==>标题
微软的Windows 8.1

有一个简单的解决方法(从SupeUser)启动/ B / WAIT 作品:

  ==>启动/ B / WAIT WMIC OS GET标题和放大器;回声XXX
标题
微软的Windows 8.1XXX

I have a batch file that will run several other file (lets call it procedure file) such as .bat,.exe,.py, etc...

if Not Exist JobStreamUnitTest_CreateTextPython_4-27-2015.txt (
    Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateTextFile.py || exit %errorlevel%
    copy /y nul JobStreamUnitTest_CreateTextPython_4-27-2015.txt
)

if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt (
    Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat || exit %errorlevel%
    copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)

if Not Exist JobStreamUnitTest_CreateTextConsole_4-27-2015.txt (
    Start /wait /b C:\Users\blee2\Documents\UnitTest\TestConsole.exe apple || exit %errorlevel%
    copy /y nul JobStreamUnitTest_CreateTextConsole_4-27-2015.txt
)

if Not Exist JobStreamUnitTest_HelloWorld_4-27-2015.txt (
    Start /wait /b C:\Users\blee2\Documents\UnitTest\HelloWorld.bat || exit %errorlevel%
    copy /y nul JobStreamUnitTest_HelloWorld_4-27-2015.txt
)

So basically, the batch file will check if the following file need to be run based on the existence of dummy file associate with each of the procedure file. This will prevent us from running successfully run if we are to run the batch file the second time.

If there is no error in any of the procedure file then the code will work fine.

The exit error will only work if the file/filepath is incorrect. The problem I am facing is that, since the Start /wait /b will always execute regardless of if one of my procedure file have an error. Therefore the exit %errorlevel% would not be run.

How do I allow the batch file to detect an error if a procedure file is broken? I would like to exit/terminal the batch file if one of the procedure file is not working. Any thoughts?

PS. /wait is needed because the start should be running in a sequential order.
/b is needed or else the program will stop after running a .bat ; /b allow us to run the batch file in the same cmd window.

Appreciate any help and thank you

Edited: The code would work if i do the following. But I am hoping to have a consistency format in my batch file, since the batch file is being generated by C# with parsing of .xml files.

if Not Exist JobStreamUnitTest_CreateTextPython_4-27-2015.txt (
    C:\Users\blee2\Documents\UnitTest\CreateTextFile.py || exit %errorlevel%
    copy /y nul JobStreamUnitTest_CreateTextPython_4-27-2015.txt
)

if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt (
    Start /wait /b C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat || exit %errorlevel%
    copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)

解决方案

I have found some issues in start /WAIT /B any_program || exit %errorlevel%:

  • #1 - %errorlevel% variable will be expanded at parse time. Thus your script never returns proper exit code. See EnableDelayedExpansion.
  • #2 - || conditional command execution: unfortunately I can't document it properly, but all my attempts with it failed in relation to start command...

IMHO next code snippet (the only example) could work as expected:

if Not Exist JobStreamUnitTest_CreateTextBatch_4-27-2015.txt (
    start /B /WAIT C:\Users\blee2\Documents\UnitTest\CreateNewFile.bat
    SETLOCAL enabledelayedexpansion
    if !errorlevel! NEQ 0 exit !errorlevel!
    ENDLOCAL
    copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)

  • #3 - a bug in the implementation of the start command.

start /WAIT /B doesn't work (the /wait argument is ignored):

==>start /WAIT /B wmic OS GET Caption & echo xxx
xxx

==>Caption
Microsoft Windows 8.1

There's a simple workaround (from SupeUser) as start /B /WAIT works:

==>start /B /WAIT wmic OS GET Caption & echo xxx
Caption
Microsoft Windows 8.1

xxx

这篇关于启动/等待/ B时出现错误不退出程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆