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

Author Topic: Do you really need EXIT at the end of a BAT file?  (Read 4419 times)

0 Members and 1 Guest are viewing this topic.

slack7639

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    Do you really need EXIT at the end of a BAT file?
    « on: January 02, 2018, 01:31:18 PM »
    I leave off EXIT, and the BAT file works fine.  I don't notice any problems.

    Do I really need to have EXIT at the end of a BAT file?

    patio

    • Moderator


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • Yes
    • Experience: Beginner
    • OS: Windows 7
    Re: Do you really need EXIT at the end of a BAT file?
    « Reply #1 on: January 02, 2018, 03:15:01 PM »
    Why would you start a new Topic where noone knows what the issue is ? ?
    " Anyone who goes to a psychiatrist should have his head examined. "

    Salmon Trout

    • Guest
    Re: Do you really need EXIT at the end of a BAT file?
    « Reply #2 on: January 02, 2018, 03:51:56 PM »
    If you start a batch script (file) from an already open command window (by typing its name and pressing ENTER), if the batch ends with EXIT, that window will close. Without an EXIT, the window will stay open.

    If you start a batch by double clicking it in Windows Explorer, the window will close when the batch ends, whether there is an EXIT or not.

    If you follow EXIT with a number, that is the exit code the script will leave behind as an errorlevel.

    If you don't need any of this, you can omit EXIT.



    slack7639

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: Do you really need EXIT at the end of a BAT file?
      « Reply #3 on: January 03, 2018, 11:26:44 AM »
      Thanks!  When do you ever want to set an "exit code"? . . . Also, if the .BAT file ran, why would there be an error?

      https://ss64.com/nt/exit.html

         exitCode   Sets the %ERRORLEVEL% to a numeric number.
                    If quitting CMD.EXE, set the process exit code no.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Do you really need EXIT at the end of a BAT file?
      « Reply #4 on: January 03, 2018, 11:42:04 AM »
      Exit codes are useful for creating batch files that are expected to be called by other batch files. The Exit Code can return information that can be evaluated by the calling batch file, just as you can do with internal and external commands.
      I was trying to dereference Null Pointers before it was cool.

      Salmon Trout

      • Guest
      Re: Do you really need EXIT at the end of a BAT file?
      « Reply #5 on: January 03, 2018, 12:38:22 PM »
      Thanks!  When do you ever want to set an "exit code"? . . . Also, if the .BAT file ran, why would there be an error?

      Don't think of it as always an indication of an "error". Sometimes it is just an "exit code". Historically, going right back to MS-DOS in the Microsoft world, programs and scripts have been able to leave behind a code, that is a number, in memory, which another script or program can read. It was called "ERRORLEVEL" and the name has stuck.

      Here are two simple example scripts. Put them both in the same folder and run test1.bat

      (1) test1.bat
      Code: [Select]
      @echo off
      echo Running test2.bat...
      call test2.bat
      echo Back from test2.bat
      if %errorlevel% equ 255 echo Answer was "yes"
      if %errorlevel% equ 128 echo Answer was "no"
      pause

      (2) test2.bat
      Code: [Select]
      @echo off
      :loop
      set /p a="Are you happy (answer y or n)? "
      if /i "%a%"=="y" exit /B 255
      if /i "%a%"=="n" exit /B 128
      echo Answer y or n!
      goto loop


      Salmon Trout

      • Guest
      Re: Do you really need EXIT at the end of a BAT file?
      « Reply #6 on: January 03, 2018, 01:08:47 PM »
      You can catch errorlevels returned by other languages:

      Put these in the same folder as each other and run test3.bat

      (1) stringlen.vbs
      Code: [Select]
      wscript.quit len(wscript.arguments(0))

      (2) test3.bat
      Code: [Select]
      @echo off
      :loop
      set /p str="Enter a string: "
      if "%str%"=="" echo String cannot be empty & goto loop
      wscript stringlen.vbs %str%
      echo String has %errorlevel% character(s)
      pause

      Salmon Trout

      • Guest
      Re: Do you really need EXIT at the end of a BAT file?
      « Reply #7 on: January 03, 2018, 03:51:35 PM »
      This would be better... copes when string has spaces...

      (2) test3.bat
      Code: [Select]
      @echo off
      :loop
      set /p str="Enter a string: "
      if "%str%"=="" echo String cannot be empty & goto loop
      wscript stringlen.vbs "%str%"
      echo String has %errorlevel% character(s)
      pause