Start /wait /b not exiting program when there is a

2019-01-28 15:40发布

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
)

1条回答
smile是对你的礼貌
2楼-- · 2019-01-28 15:58

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
查看更多
登录 后发表回答