1.

Solve : How to return success/failure from a batch file??

Answer»

Hello,

I am new to the DOS world. Could SOMEONE please help with these questions:

How do I return 0 for SUCCESS ate the end of an MSDOS batch file?
Similarly, how do I return 1 (or other values) representing erroneous execution?

Thanks in advance!

Gaborheres an example

echo off
setlocal enabledelayedexpansion
ping 127.0.0.1
if "%errorlevel%"=="0" cls &Echo Success.
if "%errorlevel%"=="1" cls &Echo Fail
endlocal
Thanks, but that's not exactly what I had in mind. Let me try to explain it in a different way:
a.bat calls b.bat and when b.bat completes, a.bat continues with steps depending on whether b.bat succeeded or failed.

a.bat:

Code: [Select]rem some code here
call b.bat
if "%errorlevel%=="0" goto success
:failure
rem do something
goto end
:success
rem do something else
:end
What would b.bat LOOK like for a.bat to work?

Thanks again!

GaborIf 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.

What is wrong with the code you provided below?

FB Quote from: fireballs on September 09, 2008, 04:23:57 PM

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
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: Sidewinder on September 09, 2008, 06:12:06 PM
Quote from: fireballs on September 09, 2008, 04:23:57 PM
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
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.

 

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

 

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 PM
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
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.

 

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 AM
Quote 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?
Similarly, how do I return 1 (or other values) representing erroneous execution?

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

echo off
setlocal enabledelayedexpansion
ping 127.0.0.1
if "%errorlevel%"=="0" cls &Echo Success.
if "%errorlevel%"=="1" cls &Echo Fail
endlocal



Discussion

No Comment Found