6

I'm newbie in using batch on Windows and have a question about the use of errorlevel.

I referenced TechNet(Exit) and many examples on google.

Most of them used /b with %errorlevel% like this

if errorlevel 1 exit /b %errorlevel%

I wonder the difference between

if errorlevel 1 exit /b

and

if errorlevel 1 exit /b %errorlevel%

I think there are no difference because %errorlevel% is not changed. Am I wrong?

Jon
  • 413,451
  • 75
  • 717
  • 787
jwchoi
  • 537
  • 6
  • 13

2 Answers2

6

TL;DR

Most of the time there should be no difference, but technically exit /b %errorlevel% is strictly worse than exit /b if what you want is to exit without changing the error level.

Analysis

EXIT /B without the optional errorlevel parameter does not change the error level, so as a standalone command it is exactly equivalent to EXIT /B %errorlevel% provided that %errorlevel% resolves to the current error level.

But there are cases where it might not do so:

  • If an environment variable named ERRORLEVEL is defined then %errorlevel% always resolves to its value (which can be arbitrary), and not to the current error level.
  • If command extensions are disabled then %errorlevel% will never resolve to the current error level (it will still read the value of the environment variable with that name, if defined). You can verify this by starting a command prompt with CMD /E:OFF and trying ECHO %errorlevel%.
  • The current error level value as produced by %errorlevel% will be fixed at the time the command is parsed, not at the time execution reaches that expression. This can result in the wrong value being produced for more complex commands. Example:

    copy j:\not_existing q:\not_existing & echo %errorlevel%
    

    This will not produce the same result as

    copy j:\not_existing q:\not_existing
    echo %errorlevel%
    

    because in the first case %errorlevel% will not produce the updated error level caused by the failed copy.

Jon
  • 413,451
  • 75
  • 717
  • 787
  • The last item could be solved by enabling and applying [delayed variable expansion](https://ss64.com/nt/delayedexpansion.html) using `copy j:\not_existing q:\not_existing & echo !errorlevel!`… – aschipfl Feb 18 '21 at 09:16
-5

None of your code makes sense. None of them do anything.

Type

if /?

and

exit /?

I've changed your code to run by changing errorlevel to 0. This is the output.

C:\Users\user>if errorlevel 0 /b 0
'/b' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\user>if errorlevel 0 /b
'/b' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\user>if errorlevel 0 /b 9009
'/b' is not recognized as an internal or external command,
operable program or batch file.

Maybe instead of searching the internet you could learn the rules.

phd443322
  • 443
  • 2
  • 3
  • Now someone has edited your question, %errorlevel% is expanded when the line is read. Any changes to errorlevel you make won't affect that line. – phd443322 Jun 11 '14 at 09:13
  • I edited the question; it's obvious that we are talking about `exit /b` here (e.g. that's what the TechNet article is about). – Jon Jun 11 '14 at 09:19
  • `if errorlevel ` is not the same as `if %errorlevel%=`. You NEVER use %errorlevel% as a substitute for `if errorlevel`. Most of the time it works the same. As another poster noted it is not always the same. Even if it is not changed there are subtle differences in behaviour. – phd443322 Jun 11 '14 at 09:38
  • The commands I used are just part of batch commands. I know that to use 'if' or 'exit' stand alone does not make sense. There must be some other commands that affect the '%errorlevel%'. – jwchoi Jun 11 '14 at 10:40
  • Sorry, but this 'answer' doesn't add anything helpful to the discussion, and belittling a newbie (both to SO & the batch subject matter) by telling them to just "read the rules" doesn't seem to help. Clearly they've done some research, as evidenced by their techNet link (which includes the '/?' help parameter), and had a legitimate question about the behavior which was not covered by the article or the command line help. -1 – johnny Mar 08 '16 at 14:09