Page 1 of 2

Exit /B %ERRORLEVEL% ???

Posted: 26 Mar 2012 21:09
by tinfanide
Could I ask a bit about the use of ERRORLEVEL in DOS?
In the EXIT case,

Code: Select all

EXIT /B 1

Safely setting the ERRORLEVEL to 1. What does it mean?

Re: Exit /B %ERRORLEVEL% ???

Posted: 26 Mar 2012 23:54
by foxidrive
Errorlevel is a thing that can be checked after a program finishes, and is a number.

Each program can use numbers to indicate if a specific error occurred, and also, any given number doesn't always indicate the same thing but is specific to the program.

If the errorlevel is positive it shows that something out of the ordinary happened.

EG: in the program widgets.exe
errorlevel 1 may mean disk write failed
errorlevel 2 may mean no media disk is present
errorlevel 3 may mean input string is missing


When errorlevel is zero it means that the program ended successfully with no errors.

Not all programs will set an errorlevel - it is entirely up to the programmer as to whether he or she uses them.

Exit /b %number%

gives you as the programmer a means to set the errorlevel.

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 04:54
by tinfanide
But does the command

Code: Select all

EXIT /B 1

set the errorlevel or it uses the errorlevel?

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 05:39
by Squashman
tinfanide wrote:But does the command

Code: Select all

EXIT /B 1

set the errorlevel or it uses the errorlevel?

Does this answer your question

Code: Select all

H:\>cmd.exe
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

H:\>exit /b 1

H:\>echo %errorlevel%
1

H:\>cmd.exe
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

H:\>exit /b 2

H:\>echo %errorlevel%
2

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 06:08
by tinfanide
Well, what I don't understand is
In your example,
Are we using the default setting of ERRORLEVEL of the command EXIT?
Or
setting the ERRORLEVEL for EXIT?

And I tried your example in CMD
When I typed

Code: Select all

EXIT /B 1

It exits.

And in your example,
Are you setting the ERRORLEVEL (1,2) for EXIT?

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 06:13
by Squashman
You did not follow my example. I ran another instance of cmd.exe first so that it wouldn't exit out my first cmd window

If you read the help for the EXIT command it states it is setting the ERRORLEVEL to the specified number!

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 06:18
by Squashman
Let try another example.
Let say we just have this simple batch file

Code: Select all

echo squashman
exit /b 2

When we execute it and I then echo the errrolevel at the cmd prompt we get this output.

Code: Select all

H:\>elevel.bat

H:\>echo Squashman
Squashman

H:\>exit /b 2

H:\>echo %errorlevel%
2

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 06:31
by tinfanide
Yes, I started another cmd.exe and it worked.
But what's the use of specifying a number for the command EXIT?

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


Could ya explain a bit for the use of this?

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 07:00
by foxidrive
tinfanide wrote:But what's the use of specifying a number for the command EXIT?


Read the last few lines of my post.

I don't think you comprehend that it sets the errorlevel when the batch file finishes in this case.

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 07:22
by tinfanide
Well, how about this?

Code: Select all

:: A batch file

ECHO it's a batch file

EXIT /B 1


Is it what you said I set an errorlevel for this batch program?

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 09:53
by foxidrive
Yes. You can make the errorlevel anything, and make it different depending if the batch file detects that a file is missing, for example.


One thing that could be handy is to store the errorlevel of a program that your batch file uses.
When your batch file finally completes you can return the errorlevel that you stored.


Is that clear?

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 09:57
by tinfanide
Half clear. Maybe next time when I come across a practical use of it, I may understand it better than just read people's explanation. Anyway, thanks for the explanation given.

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 10:13
by foxidrive
Here is an example.

If myfile.ini is not found then the batch file will return errorlevel 12 otherwise it will return the errorlevel of "my program.exe"


Code: Select all

@echo off
setlocal enableddelayedexpansion

if not exist "myfile.ini" (
echo myfile.ini has not been created
set err=12
)

echo Welcome to my batch file.

if exist "myfile.ini" (
"c:\folder one\my program.exe" "myfile.ini"
set err=!errorlevel!
)

exit /b %err%

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 19:35
by tinfanide
Here is a run-time test on my machine:
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\Windows\system32>CD C:\folder one

C:\folder one>DIR /B
ERRORLEVEL.bat
my program.exe
myfile.ini

C:\folder one>ERRORLEVEL.bat
Invalid parameter to SETLOCAL command
Welcome to my batch file.
Access is denied.

/*
My Windows is Win7 64bit. It says my program.exe is not a valid Win32 application.
I just create a new file and name it "my program.exe"
*/

C:\folder one>ECHO %ERRORLEVEL%
5
/*
Why %ERRORLEVEL% = 5?
*/

C:\folder one>

Re: Exit /B %ERRORLEVEL% ???

Posted: 28 Mar 2012 21:37
by Squashman
If you copied Foxidrive's script you should notice the spelling error (enabledelayedexpansion) which is why you are getting an error with the setlocal command.

Errorlevel 5 is the Access denied error.