132ikl
132ikl

Reputation: 85

Why won't this increment work in batch?

Why will this not increment? I am using this in a batch file. It does everything correctly, except it doesn't increment. What am I doing wrong?

@SET variable=1
@echo How many increments should be created?
@set /p increments=
@echo off

@FOR /l %%x in (1, 1, %increments%) do @(
@SET /A variable=variable+1
@echo %variable%
)

@ECHO Program Complete!
@pause

Edit: This is not a duplicate, this is an entirely different question.

Upvotes: 3

Views: 4751

Answers (4)

aschipfl
aschipfl

Reputation: 34899

Dennis van Gils' answer already shows you what is missing -- delayed variable expansion.

You are using the syntax set /A variable=variable+1 (which operates quite the same as delayed expansion like set /A variable=!variable!+1). However, you can use an alternative syntax to avoid need of delayed expansion: set /A variable+=1:

for /L %%x in (1, 1, %increments%) do (
    set /A "variable+=1"
)
echo %variable%

Of course the echo does no longer work within the loop if delayed expansion is disabled, so I moved it outside of the loop to show just the final result, using standard (immediate) expansion.

Upvotes: 3

lahiruhashan
lahiruhashan

Reputation: 120

Well I used both for and if in this solution.

@echo off
setlocal enabledelayedexpansion
SET /A i = 1
for /f "tokens=*" %%f in (temp.txt) do (

    IF !i!==2 echo %%f
    SET /a i+=1
)

Upvotes: 0

Henrik
Henrik

Reputation: 342

A slightly modified version of Dennis' solution above. Notice the set /a command. Of course, in this case you don't really need the var "variable", but I suppose it's an assignment where you are required to use it. In a more general setting you could need it.

@echo off

setlocal enabledelayedexpansion
set variable=1
set /p increments=How many increments should be created? 

for /l %%x in (1, 1, %increments%) do (
    echo !variable!
    set /a variable += 1
)

echo Program Complete!

Upvotes: 0

Dennis van Gils
Dennis van Gils

Reputation: 3452

Try this:

@echo off
setlocal EnableDelayedExpansion
SET "variable=1"
set /p "increments=How many increments should be created? "
echo off

FOR /l %%x in (1, 1, %increments%) do (
SET /A "variable=!variable!+1"
echo !variable!
)

ECHO Program Complete!
pause

Within a loop (or if) you should use setlocal EnableDelayedExpansion and ! instead of %

I also changed your set /p, because you can give a string to echo to set /p itself instead of using echo above it, and put double quotes around your sets so it will not error for values like spaces. Note that when you try to enter a string instead of a number it will simply not loop, instead of crashing

EDIT:

an easier version with only the loop-variables themselves (%%x) and without delayed expansion:

@echo off
SET "variable=1"
set /p "increments=How many increments should be created? "
set /a "forincrements=increments+1"
FOR /l %%x in (%variable%, 1, %forincrements%) do (
  echo %%x
)
ECHO Program Complete!
pause

Upvotes: 4

Related Questions