| 1. |
Solve : How to return success/failure from a batch file?? |
|
Answer» Hello, If one of b.bat's commands fails because of an error then it will set errorlevel to 1 and exit the batch program, returning to a.bat. Not quite. Not all MS commands fail with errorlevel 1. XCOPY, for instance can fail with errorlevels 1 to 5. This type of compare ("%errorlevel%=="0") becomes dubious at best. B.bat can use the exit statement to pass a return code (errorlevel) back to a.bat. Quote Quits the CMD.EXE program (command interpreter) or the current batch Quote from: Sidewinder on September 09, 2008, 06:12:06 PM Quote from: fireballs on September 09, 2008, 04:23:57 PMIf one of b.bat's commands fails because of an error then it will set errorlevel to 1 and exit the batch program, returning to a.bat. yes there are instances where the errorlevel won't be 1 choice returns 254 if there's an error. exit requires that you use the same if error gtr 0 but with exit as the command FB Quote exit requires that you use the same if error gtr 0 but with exit as the command Don't really understand this. I was thinking more along the line where b.bat would abort EARLY based on some condition: b.bat Code: [Select]if not exist c:\file.ext exit 7 if not defined userprofile exit 9 exit 0 a.bat could the query the errorlevel and proceed accordingly, which is what I interpreted the OP requested. Quote from: Sidewinder on September 09, 2008, 06:51:56 PM Quoteexit requires that you use the same if error gtr 0 but with exit as the command sorry i've beed drinking so my post contained several spelling mistakes, what i meant is that you'd still have to SPECIFY under what conditions to exit with a specific exit condition using the if command. it requires a finite amount of things that could be an error. if you use Code: [Select]if errorlevel gtr 0 exit /b [1] anything over errorleve==1 would exit with exit code 1 FByou can use: Code: [Select]&& if success || if fail example: Code: [Select]set /a var=1/0 && echo A set /a var=1/0 || echo A Quote from: Sidewinder on September 09, 2008, 06:12:06 PM Quote from: fireballs on September 09, 2008, 04:23:57 PMIf one of b.bat's commands fails because of an error then it will set errorlevel to 1 and exit the batch program, returning to a.bat. That's exactly what I was looking for! Thanks a lot! Works like a charm! Gabor C:\>A.bat "Hello World" "Call B.bat" "We are in B.bat." "Return exit code from B.bat" "errorlevel=77" 77 Press any key to continue . . . C:\>type A.bat ECHO OFF echo "Hello World" echo "Call B.bat" Call B.bat echo "Return exit code from B.bat" echo "errorlevel=%errorlevel%" echo %errorlevel% pause C:\>type B.bat REM return exit code to Batch A.bat ECHO OFF echo "We are in B.bat." exit /B 77 C:\> a little late to the party, methinks... Quote Reply #9 on: September 10, 2008, 09:45:23 AMQuote from: grevesz on September 09, 2008, 02:31:33 PM How do I return 0 for success ate the end of an MSDOS batch file? The most direct way is via exit /b value, However, in Windows 8.1 at least, that doesn't support && and || in the invoking command. To make those operators work, exit via the end of the batch file, where you place a cmd /c exit value, e.g., Code: [Select]echo off setlocal set E_FAIL=2147500037 set exit_code=%E_FAIL% set /p a=Pretend to succeed (y/n)? if "%a%"=="y" goto succeed :fail set exit_code=%E_FAIL% & goto finish :succeed set exit_code=0 & goto finish :finish cmd /c exit %exit_code% For example, if that file is called cmd_status.bat, then you can test it with Code: [Select]cmd_status && echo OK || echo Bah The Topic is 6 Years old... Don't think he'll return. Quote from: diablo416 on September 09, 2008, 03:25:36 PM heres an example |
|