Saved Bookmarks
| 1. |
Solve : DOS IF %ERRORLEVEL% construct? |
|
Answer» 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 Code: [Select]@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:\> if /? where compare-op may be one of: EQU - equal NEQ - not equal LSS - less than LEQ - less than or equal GTR - greater than GEQ - greater than or equalCode: [Select] IF %ERRORLEVEL% NEQ 0 ( ECHO "I failed" EXIT ) Or you can use GTR instead of NEQ (This is more usual) Quote from: Salmon Trout on September 02, 2009, 09:00:08 AM Code: [Select] Where is the test code and the output? Did they go fishing?http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true Quote "Using batch filesQuote from: billrich on September 02, 2009, 09:29:06 AM 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. Code: [Select]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. Quote from: billrich on September 02, 2009, 10:05:41 AM 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 Code: [Select]IF %ERRORLEVEL% EQ 0 GOTO OK Or, if you don't like parentheses you can use LABELS and gotos Code: [Select]myprogram.exe if %errorlevel% neq 0 goto end mynextprogram.exe but this is more flexible Code: [Select]myprogram.exe if %errorlevel% neq 0 ( echo there was an error goto end ) mynextprogram.exe :end Or even these Code: [Select]myprogram.exe || goto end mynextprogram.exe :end Code: [Select]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? Quote from: billrich on September 02, 2009, 12:49:43 PM Thank you Mr. Trout. 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 Quote from: BC_Programmer on September 02, 2009, 02:49:31 PM
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. |
|