Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to return success/failure from a batch file?  (Read 167862 times)

0 Members and 1 Guest are viewing this topic.

grevesz

    Topic Starter


    Starter

    How to return success/failure from a batch file?
    « on: September 09, 2008, 02:31:33 PM »
    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!

    Gabor

    diablo416



      Hopeful
      Re: How to return success/failure from a batch file?
      « Reply #1 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

      grevesz

        Topic Starter


        Starter

        Re: How to return success/failure from a batch file?
        « Reply #2 on: September 09, 2008, 04:20:08 PM »
        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!

        Gabor

        fireballs



          Apprentice

        • Code:Terminal
        • Thanked: 3
          Re: How to return success/failure from a batch file?
          « Reply #3 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.

          What is wrong with the code you provided below?

          FB
          Next time google it.

          Sidewinder



            Guru

            Thanked: 124
          • Experience: Familiar
          • OS: Windows 10
          Re: How to return success/failure from a batch file?
          « Reply #4 on: September 09, 2008, 06:12:06 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.

           8)
          The true sign of intelligence is not knowledge but imagination.

          -- Albert Einstein

          fireballs



            Apprentice

          • Code:Terminal
          • Thanked: 3
            Re: How to return success/failure from a batch file?
            « Reply #5 on: September 09, 2008, 06:20:39 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.

             8)

            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
            Next time google it.

            Sidewinder



              Guru

              Thanked: 124
            • Experience: Familiar
            • OS: Windows 10
            Re: How to return success/failure from a batch file?
            « Reply #6 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.

             8)
            The true sign of intelligence is not knowledge but imagination.

            -- Albert Einstein

            fireballs



              Apprentice

            • Code:Terminal
            • Thanked: 3
              Re: How to return success/failure from a batch file?
              « Reply #7 on: September 09, 2008, 06:57:18 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.

               8)

              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

              FB
              Next time google it.

              devcom



                Apprentice

                Thanked: 37
                Re: How to return success/failure from a batch file?
                « Reply #8 on: September 10, 2008, 01:12:38 AM »
                you 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
                Download: Choice.exe

                grevesz

                  Topic Starter


                  Starter

                  Re: How to return success/failure from a batch file?
                  « Reply #9 on: September 10, 2008, 10:45:23 AM »
                  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.

                   8)

                  That's exactly what I was looking for!
                  Thanks a lot!
                  Works like a charm!

                  Gabor

                  billrich

                  • Guest
                  Re: How to return success/failure from a batch file?
                  « Reply #10 on: March 02, 2009, 12:57:11 PM »

                  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:\>

                  BC_Programmer


                    Mastermind
                  • Typing is no substitute for thinking.
                  • Thanked: 1079
                    • Yes
                    • Yes
                    • BC-Programming.com
                  • Certifications: List
                  • Computer: Specs
                  • Experience: Beginner
                  • OS: Windows 8
                  Re: How to return success/failure from a batch file?
                  « Reply #11 on: March 02, 2009, 01:55:13 PM »
                   ::)


                  a little late to the party, methinks...

                  Quote
                  Reply #9 on: September 10, 2008, 09:45:23 AM
                  I was trying to dereference Null Pointers before it was cool.

                  alfps



                    Newbie

                    • Experience: Expert
                    • OS: Windows 7
                    Re: How to return success/failure from a batch file?
                    « Reply #12 on: December 06, 2014, 08:01:33 AM »
                    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


                    patio

                    • Moderator


                    • Sage
                    • Maud' Dib
                    • Thanked: 1676
                      • Yes
                    • Experience: Beginner
                    • OS: Windows 7
                    Re: How to return success/failure from a batch file?
                    « Reply #13 on: December 06, 2014, 08:04:07 AM »
                    The Topic is 6 Years old...
                    Don't think he'll return.
                       
                     
                    " Anyone who goes to a psychiatrist should have his head examined. "

                    alabamasuch



                      Starter

                      • Experience: Guru
                      • OS: Windows 7
                      Re: How to return success/failure from a batch file?
                      « Reply #14 on: December 15, 2017, 04:09:51 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