I can't understand choice error codes of batch
If I have a code like this:
@echo off
choice /C BL /M "Clear screen or list actual directory"
if errorlevel 2 goto l
if errorlevel 1 goto b
:l
tree /f
goto final
:b
cls
goto final
:final
I know this actually works, but I'm confused about one thing about the errorlevel parts.
I wrote first, the same code, but like this:
if errorlevel 1 goto l
if errorlevel 2 goto b
And that way it won't work properly It will only remember the errorcode 1. If you press the second option is not working.
I'm really wondering why the order of the errors matters, if a batch its supposed to execute line by line, or am i wrong?
In a nutshell, what I want to understand is how the error codes work here
batch-file if-statement cmd choice
add a comment |
If I have a code like this:
@echo off
choice /C BL /M "Clear screen or list actual directory"
if errorlevel 2 goto l
if errorlevel 1 goto b
:l
tree /f
goto final
:b
cls
goto final
:final
I know this actually works, but I'm confused about one thing about the errorlevel parts.
I wrote first, the same code, but like this:
if errorlevel 1 goto l
if errorlevel 2 goto b
And that way it won't work properly It will only remember the errorcode 1. If you press the second option is not working.
I'm really wondering why the order of the errors matters, if a batch its supposed to execute line by line, or am i wrong?
In a nutshell, what I want to understand is how the error codes work here
batch-file if-statement cmd choice
2
The classic form withif errorlevel 1
actually is evaluated asif errorlevel is 1 or greater
so with the lower number first the 2nd if isn't executed at all.
– LotPings
Nov 27 '18 at 23:25
If errorlevel 1 If not errorlevel 2
will only fire on 1.
– CatCat
Nov 27 '18 at 23:39
2
From the Choice help file: NOTE:The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
– Squashman
Nov 27 '18 at 23:41
add a comment |
If I have a code like this:
@echo off
choice /C BL /M "Clear screen or list actual directory"
if errorlevel 2 goto l
if errorlevel 1 goto b
:l
tree /f
goto final
:b
cls
goto final
:final
I know this actually works, but I'm confused about one thing about the errorlevel parts.
I wrote first, the same code, but like this:
if errorlevel 1 goto l
if errorlevel 2 goto b
And that way it won't work properly It will only remember the errorcode 1. If you press the second option is not working.
I'm really wondering why the order of the errors matters, if a batch its supposed to execute line by line, or am i wrong?
In a nutshell, what I want to understand is how the error codes work here
batch-file if-statement cmd choice
If I have a code like this:
@echo off
choice /C BL /M "Clear screen or list actual directory"
if errorlevel 2 goto l
if errorlevel 1 goto b
:l
tree /f
goto final
:b
cls
goto final
:final
I know this actually works, but I'm confused about one thing about the errorlevel parts.
I wrote first, the same code, but like this:
if errorlevel 1 goto l
if errorlevel 2 goto b
And that way it won't work properly It will only remember the errorcode 1. If you press the second option is not working.
I'm really wondering why the order of the errors matters, if a batch its supposed to execute line by line, or am i wrong?
In a nutshell, what I want to understand is how the error codes work here
batch-file if-statement cmd choice
batch-file if-statement cmd choice
edited Nov 28 '18 at 0:28
aschipfl
19.1k92956
19.1k92956
asked Nov 27 '18 at 23:11
DaburuKaoDaburuKao
435
435
2
The classic form withif errorlevel 1
actually is evaluated asif errorlevel is 1 or greater
so with the lower number first the 2nd if isn't executed at all.
– LotPings
Nov 27 '18 at 23:25
If errorlevel 1 If not errorlevel 2
will only fire on 1.
– CatCat
Nov 27 '18 at 23:39
2
From the Choice help file: NOTE:The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
– Squashman
Nov 27 '18 at 23:41
add a comment |
2
The classic form withif errorlevel 1
actually is evaluated asif errorlevel is 1 or greater
so with the lower number first the 2nd if isn't executed at all.
– LotPings
Nov 27 '18 at 23:25
If errorlevel 1 If not errorlevel 2
will only fire on 1.
– CatCat
Nov 27 '18 at 23:39
2
From the Choice help file: NOTE:The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
– Squashman
Nov 27 '18 at 23:41
2
2
The classic form with
if errorlevel 1
actually is evaluated as if errorlevel is 1 or greater
so with the lower number first the 2nd if isn't executed at all.– LotPings
Nov 27 '18 at 23:25
The classic form with
if errorlevel 1
actually is evaluated as if errorlevel is 1 or greater
so with the lower number first the 2nd if isn't executed at all.– LotPings
Nov 27 '18 at 23:25
If errorlevel 1 If not errorlevel 2
will only fire on 1.– CatCat
Nov 27 '18 at 23:39
If errorlevel 1 If not errorlevel 2
will only fire on 1.– CatCat
Nov 27 '18 at 23:39
2
2
From the Choice help file: NOTE:The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
– Squashman
Nov 27 '18 at 23:41
From the Choice help file: NOTE:The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
– Squashman
Nov 27 '18 at 23:41
add a comment |
2 Answers
2
active
oldest
votes
C:>if /?
...
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
In other words, if errorlevel 1
executes for any errorlevel (except 0 = no error) because they're all equal to or greater than 1.
add a comment |
Just a hint, when using the %errorlevel%
variable attached to a goto label, things can be quite easy:
@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%
:choice1 B
tree /f
goto final
:choice2 L
cls
goto final
:final
pause
1
Of course!:)
– Aacini
Nov 28 '18 at 0:29
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53509652%2fi-cant-understand-choice-error-codes-of-batch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
C:>if /?
...
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
In other words, if errorlevel 1
executes for any errorlevel (except 0 = no error) because they're all equal to or greater than 1.
add a comment |
C:>if /?
...
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
In other words, if errorlevel 1
executes for any errorlevel (except 0 = no error) because they're all equal to or greater than 1.
add a comment |
C:>if /?
...
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
In other words, if errorlevel 1
executes for any errorlevel (except 0 = no error) because they're all equal to or greater than 1.
C:>if /?
...
IF [NOT] ERRORLEVEL number command
...
ERRORLEVEL number Specifies a true condition if the last program run returned
an exit code equal to or greater than the number specified.
In other words, if errorlevel 1
executes for any errorlevel (except 0 = no error) because they're all equal to or greater than 1.
answered Nov 27 '18 at 23:38
melpomenemelpomene
61.6k54994
61.6k54994
add a comment |
add a comment |
Just a hint, when using the %errorlevel%
variable attached to a goto label, things can be quite easy:
@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%
:choice1 B
tree /f
goto final
:choice2 L
cls
goto final
:final
pause
1
Of course!:)
– Aacini
Nov 28 '18 at 0:29
add a comment |
Just a hint, when using the %errorlevel%
variable attached to a goto label, things can be quite easy:
@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%
:choice1 B
tree /f
goto final
:choice2 L
cls
goto final
:final
pause
1
Of course!:)
– Aacini
Nov 28 '18 at 0:29
add a comment |
Just a hint, when using the %errorlevel%
variable attached to a goto label, things can be quite easy:
@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%
:choice1 B
tree /f
goto final
:choice2 L
cls
goto final
:final
pause
Just a hint, when using the %errorlevel%
variable attached to a goto label, things can be quite easy:
@echo off
choice /C BL /M "Clear screen or list actual directory"
goto :choice%errorlevel%
:choice1 B
tree /f
goto final
:choice2 L
cls
goto final
:final
pause
answered Nov 27 '18 at 23:57
LotPingsLotPings
19.5k61533
19.5k61533
1
Of course!:)
– Aacini
Nov 28 '18 at 0:29
add a comment |
1
Of course!:)
– Aacini
Nov 28 '18 at 0:29
1
1
Of course!
:)
– Aacini
Nov 28 '18 at 0:29
Of course!
:)
– Aacini
Nov 28 '18 at 0:29
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53509652%2fi-cant-understand-choice-error-codes-of-batch%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
The classic form with
if errorlevel 1
actually is evaluated asif errorlevel is 1 or greater
so with the lower number first the 2nd if isn't executed at all.– LotPings
Nov 27 '18 at 23:25
If errorlevel 1 If not errorlevel 2
will only fire on 1.– CatCat
Nov 27 '18 at 23:39
2
From the Choice help file: NOTE:The ERRORLEVEL environment variable is set to the index of the key that was selected from the set of choices. The first choice listed returns a value of 1, the second a value of 2, and so on. If the user presses a key that is not a valid choice, the tool sounds a warning beep. If tool detects an error condition, it returns an ERRORLEVEL value of 255. When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.
– Squashman
Nov 27 '18 at 23:41