CMD not setting variables in the same line
Moderator: DosItHelp
CMD not setting variables in the same line
Hello friends
set k=1 & set j=20 && for /l %1 in (%k%,1,%j%) do @echo %1
Output:
0 ---> zero
why is zero above command output?
Please guide me in this regard .
set k=1 & set j=20 && for /l %1 in (%k%,1,%j%) do @echo %1
Output:
0 ---> zero
why is zero above command output?
Please guide me in this regard .
Re: a problem in CMD (Please guide me)
In a batch try this:
Read that last post too.
Code: Select all
@echo off
set "k=1" & set "j=20"
for /l %%i in (%k%,1,%j%) do echo %%i
exit /b
Read that last post too.
Last edited by sambul35 on 21 Jun 2016 10:03, edited 1 time in total.
-
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
Re: a problem in CMD (Please guide me)
Because of how batch expands variables, the %k% and %j% are not set when the for loop is executed.
When a for /L loop tries to use an variable that has no value, it treats it like 0 instead, so your code is getting translated to which outputs 0 and then stops.
Put the for loop on a separate line; && is used when you only want the right side to run if the left side runs successfully, and a simple set statement will not fail.
Also, %1 through %9 are traditionally used for parameters that get passed in to scripts; you can use them for for loop variables, but it's better to use letters instead.
When a for /L loop tries to use an variable that has no value, it treats it like 0 instead, so your code is getting translated to
Code: Select all
for /l %1 in (0,1,0) do @echo %1
Put the for loop on a separate line; && is used when you only want the right side to run if the left side runs successfully, and a simple set statement will not fail.
Also, %1 through %9 are traditionally used for parameters that get passed in to scripts; you can use them for for loop variables, but it's better to use letters instead.
Re: a problem in CMD (Please guide me)
I am going to assume you are trying to do this with one line from the cmd prompt.
Code: Select all
H:\>cmd /V:ON /C "set k=1 & set j=20 & for /l %I in (!k!,1,!j!) do @echo %I"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Re: a problem in CMD (Please guide me)
There is no need to enter your commands in one line!
You either enter it with your known values already in place, (as in the first window)…
…or enter it on three lines, (as in the lower window) Takes no more key presses!
You either enter it with your known values already in place, (as in the first window)…
…or enter it on three lines, (as in the lower window) Takes no more key presses!
Re: a problem in CMD (Please guide me)
Or you could do in 2 lines:
Code: Select all
set k=1 & set j=20
for /L %i in (%k% 1 %j%) do @echo %i
Re: a problem in CMD (Please guide me)
This would work if the DOS window had been started with delayed environment variable expansion enabled.
See CMD /? for details which include
John A.
Code: Select all
set k=1 & set j=20 && for /l %I in (!k!,1,!j!) do @echo %I
One practical way of doing this is to create a shortcut to the DOS Prompt on your Desktop and change the Target: line in Shortcut Properties toCMD /? wrote:Code: Select all
Delayed environment variable expansion is NOT enabled by default.
You can enable or disable delayed environment variable expansion for a
particular invocation of CMD.EXE with the /V:ON or /V:OFF switch.
Code: Select all
%ComSpec% /V /K
John A.
Re: a problem in CMD (Please guide me)
thefeduke wrote:This would work if the DOS window had been started with delayed environment variable expansion enabled.
...
John A.
This will do the trick:
Code: Select all
start "" /B cmd /v:on /k "echo. & set k=1 & set j=20 & for /L %i in (!k!,1,!j!) do @echo %i
Re: a problem in CMD (Please guide me)
hacker wrote:set k=1 & set j=20 && for /l %1 in (%k%,1,%j%) do @echo %1
Output:
0 ---> zero
why is zero above command output?
The reason is that CMD only sets the variable at the end of a line. Run this script and you will see how that occurs.
Code: Select all
@echo off
set num=222&echo test 1 "%num%"
echo test 2 "%num%"
set k=1 & set j=20 & echo test 3 for /l %%a in (%k%,1,%j%) do @echo %%a
echo test 4 for /l %%a in (%k%,1,%j%) do @echo %%a
pause
Re: CMD not setting variables in the same line
ShadowThief wrote:When a for /L loop tries to use an variable that has no value, it treats it like 0 instead, so your code is getting translated toCode: Select all
for /l %1 in (0,1,0) do @echo %1
which outputs 0 and then stops.
Well, this is not entirely exact... This result is the combination of two factors:
- In a FOR /L command, if anyone of the "start,step,end" values is NOT a number, then it is taken as a zero value. For example: "for /L %i in (NOTNUM,1,10) do ..." would loop with values from 0 to 10 because the part "NOTNUM" is NOT a number (independently if a variable named NOTNUM may exist).
- In the command line, if a variable enclosed in percents-signs does NOT exist, then its name and the percent-signs are NOT changed. This way, in this particular example: "for /l %1 in (%k%,1,%j%) do @echo %1" the "%k%" and "%j%" parts are NOT replaced, so the values of "start" and "end" are "%k%" and "%j%" respectively, that are taken as zero because they are NOT numbers. If this FOR would be executed in a Batch file, then the "%k%" and "%j%" parts would be just removed and the FOR would not be executed at all.
_______________________________________________________________________________
I think the simplest way to execute this FOR from the command-line is this way:
Code: Select all
set k=1 & set j=20 & cmd /C for /l %i in (%k%,1,%j%) do @echo %i
... but this method only works the first time, when both "k" and "j" variables does not exists. This means that it would be necessary to delete "k" and "j" variables in order to execute the FOR again with different values.
Antonio
Re: CMD not setting variables in the same line
I agree with Aacini.
You could add a ^ to delay the expansion, so that the for loop always uses actual k,j values.
But then the variables ^k and ^j needs to be undefined, or defined as %k% or %j%.
(If prefer not using such variable names - but you cannot be sure if using foreign code.)
penpen
You could add a ^ to delay the expansion, so that the for loop always uses actual k,j values.
But then the variables ^k and ^j needs to be undefined, or defined as %k% or %j%.
(If prefer not using such variable names - but you cannot be sure if using foreign code.)
Code: Select all
set k=1 & set j=20 & cmd /C for /l %i in (%^k%,1,%^j%) do @echo %i
penpen
Re: CMD not setting variables in the same line
I changed this reply as I missed reading ShadowThief's comments well enough.
Re: CMD not setting variables in the same line
Aacini wrote:I think the simplest way to execute this FOR from the command-line is this way:Code: Select all
set k=1 & set j=20 & cmd /C for /l %i in (%k%,1,%j%) do @echo %i
... but this method only works the first time, when both "k" and "j" variables does not exists. This means that it would be necessary to delete "k" and "j" variables in order to execute the FOR again with different values.
Antonio
In my testing it works every time Antonio, but I expect you meant that changing the variables causes a little hiccup.
It does work when changing the variables in my test too, but it has to be run twice and on the second time it returns the correct changed values.