Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#1
Post
by tinfanide » 26 Mar 2012 21:09
Could I ask a bit about the use of ERRORLEVEL in DOS?
In the EXIT case,
Safely setting the ERRORLEVEL to 1. What does it mean?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#2
Post
by foxidrive » 26 Mar 2012 23:54
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.
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#3
Post
by tinfanide » 28 Mar 2012 04:54
But does the command
set the errorlevel or it uses the errorlevel?
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 28 Mar 2012 05:39
tinfanide wrote:But does the command
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
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#5
Post
by tinfanide » 28 Mar 2012 06:08
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
It exits.
And in your example,
Are you setting the ERRORLEVEL (1,2) for EXIT?
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#6
Post
by Squashman » 28 Mar 2012 06:13
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!
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#7
Post
by Squashman » 28 Mar 2012 06:18
Let try another example.
Let say we just have this simple batch file
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
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#8
Post
by tinfanide » 28 Mar 2012 06:31
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?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 28 Mar 2012 07:00
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.
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#10
Post
by tinfanide » 28 Mar 2012 07:22
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?
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#11
Post
by foxidrive » 28 Mar 2012 09:53
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?
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#12
Post
by tinfanide » 28 Mar 2012 09:57
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.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#13
Post
by foxidrive » 28 Mar 2012 10:13
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%
-
tinfanide
- Posts: 117
- Joined: 05 Sep 2011 09:15
#14
Post
by tinfanide » 28 Mar 2012 19:35
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>
-
Squashman
- Expert
- Posts: 4488
- Joined: 23 Dec 2011 13:59
#15
Post
by Squashman » 28 Mar 2012 21:37
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.