1

Code is self explanatory. I have tried the commands in the commented lines with equal results. Last lines are test of incremental assignment and evidence enabledelayedexpansion works. The fault must lie within the for loop.

@echo off
setlocal EnableDelayedExpansion
set count_k=5

for /l %%a in (1,1,5) do (
rem set a/ count_k+=1
rem set a/ "count_k+=1"
set a/ count_k=count_k+1
echo This is count_k per   %count_k%
echo This is count_k exc  !count_k!
)

echo After loop this is count_k %count_k%

set _var=first
set _var=second & echo %_var% !_var!

set count = 0
(
set /a count+=1
echo %count% fails
echo !count! works
)

This is the output of the above batch file:

This is count_k per   5
This is count_k exc  5
This is count_k per   5
This is count_k exc  5
This is count_k per   5
This is count_k exc  5
This is count_k per   5
This is count_k exc  5
This is count_k per   5
This is count_k exc  5
After loop this is count_k 5
first second
 fails
1 works
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Neo_Ace
  • 23
  • 3

2 Answers2

1

I have never seen the "a/" parameter to the "set" command before. Are you sure it is not intended to be "/a", which could be hosing your results and your code?

I hate to hand out fish instead of teaching to fish, but is this similar to what you are trying to do?

@ECHO OFF
SET COUNT=0
ECHO Before the loop count is: %COUNT%

FOR /L %%A IN (1,1,5) DO (
  @ECHO Loop %%A
  SET /A COUNT=%COUNT%+%%A
)

ECHO Outside the loop count is: %COUNT%

The output looks like this:

Before the loop count is: 0
Loop 1
Loop 2
Loop 3
Loop 4
Loop 5
Outside the loop count is: 5

By REMarking out the @ECHO OFF, it looks like this:

C:\Users\loginID>REM @ECHO OFF

C:\Users\loginID>SET COUNT=0

C:\Users\loginID>ECHO Before the loop count is: 0
Before the loop count is: 0

C:\Users\loginID>FOR /L %A IN (1 1 5) DO (

 SET /A COUNT=0+%A
)

C:\Users\loginID>(

 SET /A COUNT=0+1
)
Loop 1

C:\Users\loginID>(

 SET /A COUNT=0+2
)
Loop 2

C:\Users\loginID>(

 SET /A COUNT=0+3
)
Loop 3

C:\Users\loginID>(

 SET /A COUNT=0+4
)
Loop 4

C:\Users\loginID>(

 SET /A COUNT=0+5
)
Loop 5

C:\Users\loginID>ECHO Outside the loop count is: 5
Outside the loop count is: 5

Notice that inside the loop, the reference to %COUNT% is always 0 (from before entering the loop) no matter how many times we reset it inside the loop. If I make a change inside the loop to

SET /A COUNT+=%%A

My final result echoing %COUNT% will result in 15. Beyond this, I am not sure what you are trying to achieve.

Daniel Liston
  • 230
  • 1
  • 10
0

This works with Windows 7 console window:

@echo off
setlocal EnableDelayedExpansion
set count_k=5
for /l %%a in (1 1 5) do (
set /a count_k=!count_k!+1
echo !count_k! 
)

Note that echo %count_k% will output 5, since %...% is similar to C preprocessor that does string substitutions before actually running the batch file.

rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • 1
    just curious: why do you start every line with `@` despite of `@echo off`? – Stephan Jul 28 '17 at 11:37
  • @Stephan - Just habit, as you mentioned it wasn't needed. I often cut and paste the batch files, some of which don't have the echo off. It's fixed now. – rcgldr Jul 28 '17 at 15:20