Squaring step variable - Batch for loop - batch-file

Is it possible to square the increment/step variable in a batch for loop.
This is what it looks like right now
FOR /L %%A IN (1024,1000, 1048576) DO (
do stuff
)
however instead of going up by 1000 each time, I want to go up by 2^10, 2^11 .... 2^20 (1048576) is it possible to do that?

No, for /l loops can not handle geometric increments. But you can use batch arithmetics for it
for /l %%a in (10 1 20) do (
set /a "A=1<<%%a"
setlocal enabledelayedexpansion
for %%A in (!A!) do ( endlocal
rem Your code here - do stuff
echo %%A
)
)
Delayed expansion is needed to handle the changing variable inside the block of code. If your inner code has no problems with delayed expansion being active, it can be simplified as
setlocal enabledelayedexpansion
for /l %%a in (10 1 20) do (
set /a "A=1<<%%a"
rem Your code here - do stuff
echo !A!
)

If switching to PowerShell is fine for you, you can use
foreach ($i in 1..3) {
# example output
echo "1000^$i = $([Math]::Pow(1024, $i))"
# start "myprogram.bat --parameter $x
& 'myprogram.bat' #("--parameter", [Math]::Pow(1024, $i))
}

Related

Problems with for loops in batch file

I am trying to create a for loop in batch with some calculation using modulus from 1-100 then return an average for the entire set of numbers. I want to print this average on the screen and to a file called output This is what i have but it doesn't seem to be working correctly. Any suggestions? Thank you kindly.
#echo off
setlocal EnabledDelayedExpansion
for /l %%i in (1,1,100) do (
set /a EXPR = %%i %% 5
set /a EXPR2 = %EXPR+3
set /a TOTAL = TOTAL+%EXPR2
)
set /a AVG = %TOTAL/100
echo Your average is %AVG
echo Your average is %AVG >> output.txt
#echo off
setlocal EnableDelayedExpansion
for /l %%i in (1,1,100) do (
set /a EXPR=%%i %% 5
set /a EXPR2=EXPR+3
set /a TOTAL=TOTAL+EXPR2
)
set /a AVG=TOTAL/100
echo Your average is %AVG%
echo Your average is %AVG%
Is this what you want?
One additional d in EnableDelayedExpansion (btw. set /a works also without delayed expansion). Classical error is to let spaces surrounding variable assignment.In batch scripts result is that the space becomes part of the variable name.Except FOR tokens and command line arguments variables are two side enclosed with %

Batch - For Loop 1-100, mod 5 for each

Batch for a class...Can't figure this out. Currently have
:forLoop
echo.
for /L %%x in (1, 1, 100) do (
SET /A result=%%x %% 2
echo %%x
)
Any ideas guys?
Edit: I'm trying to use a for loop to iterate from 1 to 100. Then in the for loop obtain modulus of 1,2,3,4, etc. So in my example it would be modulus x. Where x is constantly incrementing.
Try this:
SETLOCAL ENABLEDELAYEDEXPANSION
:forLoop
echo.
for /L %%x in (1, 1, 100) do (
SET /A result=%%x %% 2
echo !result!
)
The first command is necessary for the variables to be expanded at execution time.
Delayed expansion (at run-time) of the variable is performed with the ! symbol.
If you use the %, you get the parse-time value.
EDIT:
Considering Stephan suggestion, i see that we can use call for a shorter solution:
for /L %%x in (1, 1, 100) do (
set /A result="%%x %% 2"
call echo %%result%%
)

sum numbers in for loop, batch file

I need to make a for loop that sums up numbers from 1 to x where x is users input number.
I know how to make the for loop and display those numbers but I don't know how to sum them at the same time.
FOR /L %%x IN (1,1,%x%) DO ( echo %%x
)
to do arithmetics, use set /a. Also use delayed expansion to use the variable inside a block (between ( and ))
setlocal enabledelayedexpansion
set sum=0
FOR /L %%x IN (1,1,%x%) DO (
set /a sum=!sum!+%%x
echo + %%x = !sum!
)

using index in a path in Batch

I am trying to use the index in a for loop and store it in a variable.
I wrote a batch file with this code:
FOR /L %%x IN (1,1,3) DO (
ECHO %%x
SET tmp=prefix_%%x.suffix
ECHO tmp is %tmp%
)
when running it, I get:
1
tmp is prefix_3.suffix
2
tmp is prefix_3.suffix
3
tmp is prefix_3.suffix
i would expect it to be:
1
tmp is prefix_1.suffix
2
tmp is prefix_2.suffix
3
tmp is prefix_3.suffix
what am I doing wrong?!
Setlocal enableextensions enabledelayedexpansion
FOR /L %%x IN (1,1,3) DO (
ECHO %%x
SET tmp=prefix_%%x.suffix
ECHO tmp is !tmp!
)
All the block in the for command is evaluated when it is readed. %%x changes are seen as it is a special variable known to change during loops, but %tmp% is only translated once, at the start and is not reevaluated. Enabling delayed expansion and changing sintax to !tmp! instructs cmd to reevaluate the variable each time it is accessed.

Use a variable in a 'for' loop

I have following code:
#echo off
SET ITER=0
for %%i in (%*) do (
SET ITER+=1
ECHO %ITER%
)
The output is (for three arguments):
0
0
0
Expected output:
1
2
3
Why can't I access the updated variable in the for loop?
Expansion of variables with percents is done before a statement/block is executed.
So in your case the complete block is expanded before the echo %ITER% is executed, to constant echo 0.
The variable ITER itself is updated in the loop properly.
To avoid this, you could use the delayed expansion, this works like percent expansion but just in the moment of execution
#echo off
setlocal EnableDelayedExpansion
SET ITER=0
for %%i in (%*) do (
SET /a ITER+=1
ECHO !ITER!
)

Resources