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%%
)
Related
I am writing a batch file with a for loop and am having trouble getting the left and mid logic working. I have successfully got this logic working outside of the for loop and have attached the code here.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a value = 1000
set /a range = "%value%"
set /a secrange = "%value%"
for /l %%x in (1, 1, 10) do (
echo %range%
set range=%range:~0,1%
set secrange=%secrange:~1,3%
echo !range!
echo !secrange!
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
The output is shown here.
Split 1000
So the above cmd bat is able to split the string and then combine it with a period mark in the middle of the two split strings.
However, in my for loop I don't achieve this result. I know it may be a syntax issue but I am not sure how to fix it (I have tried a lot of different things already).
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a range = "X"
set /a secrange = "X"
for /l %%x in (1, 1, 10000) do (
if %%x geq 1000 (
if %%x lss 10000 (
set "range=%%x"
set "secrange=%%x"
echo !range!
echo !secrange!
set "range=%range%:~0,1%"
echo !range!
echo %range%
set secrange=%secrange:~1,3%
echo !secrange!
echo %secrange%
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
REM End x greater 1000
)
REM End for loop
)
I use secrange the same way as I did in the original code without the for loop. I tried to manipulate the range portion with many different syntax combos but have not been able to output the left character 1. I only am able to output 0 for both characters.
I attached the output here. Bad Output
I understand there is an error (Echo is Off). Please ignore that it is just based off my echo output type for my personal testing.
The important two outputs are the third and fourth line (whichever one is referring to the correct syntax). Does anyone know how to fix the syntax in order to get the left character of 1000 in the for loop and the remaining 3 zeros of the 1000?
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 %
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))
}
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!
)
I am trying to multiply two variables through a batch file. The code is:
SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
SET /A res = 0
FOR /L %%i IN (1,1,2) DO (
FOR /L %%j IN (1,1,3) DO (
SET /A res = %%i * %%j
ECHO Multiplying %%i and %%j
ECHO %res%
)
)
The problem is that I always get 0 as a result. Can anyone please tell me what I am doing wrong?
Here's the output:
Multiplying 1 and 1
0
Multiplying 1 and 2
0
Multiplying 1 and 3
0
Multiplying 2 and 1
0
Multiplying 2 and 2
0
Multiplying 2 and 3
0
Thank you!
This is because you're outputting the result in the same block where you set it. Read up help set on delayed expansion. cmd expands %variables% when a command is parsed which is much earlier than when it's executed. As a side effect your %res% is replaced by zero.
You need
setlocal enabledelayedexpansion
at the start and then use !res! instead of %res%.
Side note: Ditch the spaces around the = in your set calls. They work as you expect when using them with /A but won't otherwise.
You need to use ! to expand the variable due to your delayed expansion setting:
SETLOCAL ENABLEDELAYEDEXPANSION
#ECHO OFF
SET res = 0
FOR /L %%i IN (1,1,2) DO (
FOR /L %%j IN (1,1,3) DO (
SET /A res = %%i * %%j
ECHO Multiplying %%i and %%j
ECHO !res!
)
)