HenS
HenS

Reputation: 1

How to use EnableDelayedExpansion in a FOR /L?

I keep getting the error Missing Operator. I think i'm using "!" completely wrong but I don't know where to use it.

ECHO ON

SETLOCAL EnableDelayedExpansion

SET CSV_Name=
SET /P CSV_Name=Please enter the CSV Name.

ECHO Zahl;Quadrat;Kubik >> C:\Users\Jeff\Desktop\%CSV_Name%.csv

SET Square=0
SET Cubic=0
FOR /L  %%A IN (2, 2, 100) DO (
 SET /A !Square! = %%A * %%A
 SET /A !Cubic! = %%A * %%A * %%A
 ECHO %%A
 ECHO !Square!
 ECHO %%A;!Square!;!Cubic! >> C:\Users\Jeff\Desktop\%CSV_Name%.csv
 )

Can someone explain what I do wrong?

Upvotes: 0

Views: 44

Answers (2)

Magoo
Magoo

Reputation: 80033

SET /A !Square! = %%A * %%A

will attempt to set a variable named (the contents of square)

SET /A Square = %%A * %%A

will set square.

!var! will retrieve the current contents of the variable var (ie. as it changes due to the operation of the loop) whereas %var% will retrieve the initial value of the variable - as it stood when the statement was checked for validity ("parsed"). Outside of a loop, !var! = %var% but it's conventional wisdom to use %var% and reserve !var! for a value that may change during the operation of a code block (parenthesised sequence of lines).

Upvotes: 1

npocmaka
npocmaka

Reputation: 57252

try with

 SET /A Square = %%A * %%A
 SET /A Cubic = %%A * %%A * %%A

you don't need ! or % when setting a new value to a variable.

Upvotes: 1

Related Questions