CMD not setting variables in the same line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
hacker
Posts: 6
Joined: 29 Aug 2014 06:21

CMD not setting variables in the same line

#1 Post by hacker » 21 Jun 2016 07:24

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 .

sambul35
Posts: 192
Joined: 18 Jan 2012 10:13

Re: a problem in CMD (Please guide me)

#2 Post by sambul35 » 21 Jun 2016 07:38

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.
Last edited by sambul35 on 21 Jun 2016 10:03, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: a problem in CMD (Please guide me)

#3 Post by ShadowThief » 21 Jun 2016 07:46

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

Code: Select all

for /l %1 in (0,1,0) do @echo %1
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.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: a problem in CMD (Please guide me)

#4 Post by Squashman » 21 Jun 2016 08:04

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

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: a problem in CMD (Please guide me)

#5 Post by Compo » 21 Jun 2016 08:09

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)
Image
…or enter it on three lines, (as in the lower window) Takes no more key presses!

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: a problem in CMD (Please guide me)

#6 Post by Thor » 01 Jul 2016 11:33

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

thefeduke
Posts: 211
Joined: 05 Apr 2015 13:06
Location: MA South Shore, USA

Re: a problem in CMD (Please guide me)

#7 Post by thefeduke » 01 Jul 2016 12:49

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

Code: Select all

%ComSpec% /V /K

John A.

Thor
Posts: 43
Joined: 31 Mar 2016 15:02

Re: a problem in CMD (Please guide me)

#8 Post by Thor » 01 Jul 2016 16:04

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: a problem in CMD (Please guide me)

#9 Post by foxidrive » 02 Jul 2016 19:10

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

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: CMD not setting variables in the same line

#10 Post by Aacini » 03 Jul 2016 00:07

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

Code: 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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: CMD not setting variables in the same line

#11 Post by penpen » 03 Jul 2016 03:44

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

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: CMD not setting variables in the same line

#12 Post by foxidrive » 03 Jul 2016 06:56

I changed this reply as I missed reading ShadowThief's comments well enough.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: CMD not setting variables in the same line

#13 Post by foxidrive » 03 Jul 2016 07:09

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.

Post Reply