Ok, I need to test the successful execution of a program within a DOS batch file, print if program fails but continue if program succeeds.
Pseudo-code;
program.exe # program that is executed and status to be checked
IF %ERRORLEVEL NEQ 0 ECHO "I failed" EXIT # check status
otherwise continue with batch job
....
Need code example because DOS is driving me crazy ... should be simple but I am using
myprogram.exe
@IF %ERRORLEVEL% NEQ 1 GOTO ERROR
@IF %ERRORLEVEL% EQ 0 GOTO OK
:ERROR
ECHO "Program failed, please check this log file for errors ..."
GOTO END
:OK
mynestprogram.exe
:END
and it is not working
--------------------------------------------------------------------------------
C:\>type err.bat
@echo off
rem myprogram.exe 0
set errorlevel=%1
echo errorlevel = %errorlevel%
IF %errorlevel% EQU 1 GOTO ERROR
IF %errorlevel% EQU 0 GOTO OK
:ERROR
ECHO "Program failed, please check this log file for errors ..."
GOTO END
:OK
echo mynestprogram.exe
:END
Output:
C:\>err.bat 0
errorlevel = 0
mynestprogram.exe
C:\>err.bat 1
errorlevel = 1
"Program failed, please check this log file for errors ..."
C:\>
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true
"Using batch files
With batch files, which are also called batch programs or scripts, you can simplify routine or repetitive tasks. A batch file is an unformatted text file that contains one or more commands and has a .bat or .cmd file name extension. When you type the file name at the command prompt, Cmd.exe runs the commands sequentially as they appear in the file.
You can include any command in a batch file. Certain commands, such as for, goto, and if, enable you to do conditional processing of the commands in the batch file. For example, the if command carries out a command based on the results of a condition. Other commands allow you to control input and output and call other batch files.
The standard error codes that most applications return are 0 if no error occurred and 1 (or higher value) if an error occurred. Please refer to your application help documentation to determine the meaning of specific error codes.
For more information about batch file operations, see the following topics:
Using batch parameters
Using filters
Using command redirection operators"
Where is the test code and the output? Did they go fishing?
Not really necessary, but I'll humour you. Don't swim in my river, or you'll drown.
program.exe # program that is executed and status to be checked
IF %ERRORLEVEL% NEQ 0 (
ECHO "I failed"
EXIT
)
otherwise continue with batch job
This is what Mr. Trout is fishing for:
EXIT
Quits the CMD.EXE program (command interpreter) or the current batch script.
EXIT [ /B ] [ exitCode ]
/B Specifies to exit the current batch script instead of CMD.EXE.
If executed from outside a batch script, it will quit CMD.EXE.
exitCode Specifies a numeric number.
If /B is specified, sets ERRORLEVEL that number.
If quitting CMD.EXE, sets the process exit code with that number.
This is what Mr. Trout is fishing for:
No it isn't. The OP clearly knows what the EXIT command does and also has the idea of checking errorlevel and asked how to display a message and then exit following a nonzero errorlevel.
One reason why this did not work may be because it should be EQU not EQ
IF %ERRORLEVEL% EQ 0 GOTO OK
Or, if you don't like parentheses you can use labels and gotos
myprogram.exe
if %errorlevel% neq 0 goto end
mynextprogram.exe
but this is more flexible
myprogram.exe
if %errorlevel% neq 0 (
echo there was an error
goto end
)
mynextprogram.exe
:end
Or even these
myprogram.exe || goto end
mynextprogram.exe
:end
myprogram.exe && mynextprogram.exe
Thank you Mr. Trout.
You have answered all of tale103108's questions.
Too bad tale103108 does not provide any feedback.
Are you a Guru for batch files?
lol... it's amazing, I would have thought everyone would have figured out his secret by now...
Guess it's limited to a small subset, eh Salmon
lol... it's amazing, I would have thought everyone would have figured out his secret by now...
Guess it's limited to a small subset, eh Salmon
Seems that way. You figured it out. I thought my ponderous prose style and choleric disposition would give me away to all, but it seems I have been lucky.