Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

windows - Start /wait /b not exiting program when there is an error

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:Userslee2DocumentsUnitTestCreateTextFile.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:Userslee2DocumentsUnitTestCreateNewFile.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:Userslee2DocumentsUnitTestTestConsole.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:Userslee2DocumentsUnitTestHelloWorld.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:Userslee2DocumentsUnitTestCreateTextFile.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:Userslee2DocumentsUnitTestCreateNewFile.bat || exit %errorlevel%
    copy /y nul JobStreamUnitTest_CreateTextBatch_4-27-2015.txt
)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:Userslee2DocumentsUnitTestCreateNewFile.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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...