Why does this batch variable never change even when set?
up vote
11
down vote
favorite
@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)
It seems that the variable "first" is always 0. Why?
windows batch-file
add a comment |
up vote
11
down vote
favorite
@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)
It seems that the variable "first" is always 0. Why?
windows batch-file
2
Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)
It seems that the variable "first" is always 0. Why?
windows batch-file
@echo off
SET first=0
FOR %%N IN (hello bye) DO (
SET first=1
echo %first%
echo %%N
)
It seems that the variable "first" is always 0. Why?
windows batch-file
windows batch-file
asked Oct 16 '10 at 17:31
jcao219
1,79221622
1,79221622
2
Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24
add a comment |
2
Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24
2
2
Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24
Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24
add a comment |
2 Answers
2
active
oldest
votes
up vote
23
down vote
accepted
With batch files, variables are expanded when their command is read - so that would be as soon as the for
executes. At that point, it no longer says echo %first%
, it literally says echo 0
, because that was the value at the point of expansion.
To get around that, you need to use delayed expansion by surrounding your variable name with !
instead of %
- so that would be echo !first!
. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion
in the beginning of your batch file (just after echo off
).
If you type set /?
, you'll see a much more detailed explanation of this at the end of the output.
add a comment |
up vote
-1
down vote
It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.
New contributor
That's just wrong. Although I'm interested, which "other options" you're talking about (besides/a
and/p
)
– Stephan
Nov 21 at 19:21
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
23
down vote
accepted
With batch files, variables are expanded when their command is read - so that would be as soon as the for
executes. At that point, it no longer says echo %first%
, it literally says echo 0
, because that was the value at the point of expansion.
To get around that, you need to use delayed expansion by surrounding your variable name with !
instead of %
- so that would be echo !first!
. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion
in the beginning of your batch file (just after echo off
).
If you type set /?
, you'll see a much more detailed explanation of this at the end of the output.
add a comment |
up vote
23
down vote
accepted
With batch files, variables are expanded when their command is read - so that would be as soon as the for
executes. At that point, it no longer says echo %first%
, it literally says echo 0
, because that was the value at the point of expansion.
To get around that, you need to use delayed expansion by surrounding your variable name with !
instead of %
- so that would be echo !first!
. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion
in the beginning of your batch file (just after echo off
).
If you type set /?
, you'll see a much more detailed explanation of this at the end of the output.
add a comment |
up vote
23
down vote
accepted
up vote
23
down vote
accepted
With batch files, variables are expanded when their command is read - so that would be as soon as the for
executes. At that point, it no longer says echo %first%
, it literally says echo 0
, because that was the value at the point of expansion.
To get around that, you need to use delayed expansion by surrounding your variable name with !
instead of %
- so that would be echo !first!
. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion
in the beginning of your batch file (just after echo off
).
If you type set /?
, you'll see a much more detailed explanation of this at the end of the output.
With batch files, variables are expanded when their command is read - so that would be as soon as the for
executes. At that point, it no longer says echo %first%
, it literally says echo 0
, because that was the value at the point of expansion.
To get around that, you need to use delayed expansion by surrounding your variable name with !
instead of %
- so that would be echo !first!
. This may require you to start cmd.exe with the /V parameter, or use setlocal enabledelayedexpansion
in the beginning of your batch file (just after echo off
).
If you type set /?
, you'll see a much more detailed explanation of this at the end of the output.
answered Oct 16 '10 at 17:37
Michael Madsen
46.2k66075
46.2k66075
add a comment |
add a comment |
up vote
-1
down vote
It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.
New contributor
That's just wrong. Although I'm interested, which "other options" you're talking about (besides/a
and/p
)
– Stephan
Nov 21 at 19:21
add a comment |
up vote
-1
down vote
It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.
New contributor
That's just wrong. Although I'm interested, which "other options" you're talking about (besides/a
and/p
)
– Stephan
Nov 21 at 19:21
add a comment |
up vote
-1
down vote
up vote
-1
down vote
It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.
New contributor
It's because the option isn't being specified, whether it is /a, /p, or any other option. Since you are choosing an arithmetic output, edit the command line to say SET /a first=1. This should work.
New contributor
New contributor
answered Nov 21 at 18:55
Spy_Prototype_96.1.5-B HD
11
11
New contributor
New contributor
That's just wrong. Although I'm interested, which "other options" you're talking about (besides/a
and/p
)
– Stephan
Nov 21 at 19:21
add a comment |
That's just wrong. Although I'm interested, which "other options" you're talking about (besides/a
and/p
)
– Stephan
Nov 21 at 19:21
That's just wrong. Although I'm interested, which "other options" you're talking about (besides
/a
and /p
)– Stephan
Nov 21 at 19:21
That's just wrong. Although I'm interested, which "other options" you're talking about (besides
/a
and /p
)– Stephan
Nov 21 at 19:21
add a comment |
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%2f3949978%2fwhy-does-this-batch-variable-never-change-even-when-set%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
Possible duplicate of Windows Batch Variables Won't Set
– phuclv
Mar 11 at 1:24