Page 1 of 1
CMD not setting variables in the same line
Posted: 21 Jun 2016 07:24
by hacker
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 .
Re: a problem in CMD (Please guide me)
Posted: 21 Jun 2016 07:38
by sambul35
In a batch try
this:
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.
Re: a problem in CMD (Please guide me)
Posted: 21 Jun 2016 07:46
by ShadowThief
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.
Re: a problem in CMD (Please guide me)
Posted: 21 Jun 2016 08:04
by Squashman
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)
Posted: 21 Jun 2016 08:09
by Compo
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!
Re: a problem in CMD (Please guide me)
Posted: 01 Jul 2016 11:33
by Thor
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)
Posted: 01 Jul 2016 12:49
by thefeduke
This would work if the DOS window had been started with delayed environment variable expansion enabled.
Code: Select all
set k=1 & set j=20 && for /l %I in (!k!,1,!j!) do @echo %I
See CMD /? for details which include
CMD /? 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.
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 to
John A.
Re: a problem in CMD (Please guide me)
Posted: 01 Jul 2016 16:04
by Thor
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)
Posted: 02 Jul 2016 19:10
by foxidrive
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
Posted: 03 Jul 2016 00:07
by Aacini
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 to
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
Posted: 03 Jul 2016 03:44
by penpen
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.)
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
Posted: 03 Jul 2016 06:56
by foxidrive
I changed this reply as I missed reading ShadowThief's comments well enough.
Re: CMD not setting variables in the same line
Posted: 03 Jul 2016 07:09
by foxidrive
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.