I'm new in batch and have made a program that writes a file with the %num% variable.
I'm having problems with the sum. Instead of returning 1, 2 and 3, it returns (0+1),((0+1)+1) and (((0+1)+1)+1)...
Here is the code:
set num=0
:loop
set num=(%num%+1)
echo test > "%num%".txt
pause
goto loop;
you need the /a parameter to do arithmetics:
set /a num=%num%+1
shorter:
set /a num=num+1
even shorter:
set /a num+=1
Try it like below
#echo off
set num=0
:loop
set /a num=%num%+1
echo test > %num%.txt
pause
goto loop;
Related
I would like my variable res to be updated inside the loop, but it does not update until after the second iteration.
Here is my code:
#echo off
set /a res=10
:loop
if %res% gtr 100 (
goto endLoop
) else (
set /a res=res*10
rem Here the 'res' shouldn't it be 100?
echo the result is : %res%
rem Here the first iteration display 10 not 100.
goto loop
)
:endLoop
pause > nul
Is there an explanation for this?
As an example of usage of delayed expansion here's your modified code:
#Echo Off
SetLocal EnableDelayedExpansion
Set "res=10"
:loop
If %res% Lss 100 (
Set/A res*=10
Echo the result is : !res!
GoTo loop
)
Pause>Nul
There is an alternative without using delayed expansion in this case, but I'd suggest you stick with the former until you are confident enough to understand the order in which things are read and parsed:
#Echo Off
Set "res=10"
:loop
If %res% Lss 100 (
Set/A res*=10
Call Echo the result is : %%res%%
GoTo loop
)
Pause>Nul
I have this code:
setlocal enableDelayedExpansion
set count=0
set letter=a,b,c
for %%a in (%letter%) do (
set /a "count+=1"
echo %count%
)
pause
The output is:
0
0
0
I want that the output will be:
1
2
3
I also tried to do it without EnableDelayedExpansion, but I had no luck. What did I do wrong?
you need to
echo !count!
with delayedexpansion
or
call echo %%count%%
%count% will always return the value of count as it stood when the block (parenthesised series of statements) was encountered.
Why doesn't this batch script add the value 5 to the variable %counter%?
The script always echo the value 2 with which the variable was initialized.
Outside a if statement the counter works just fine.
:start
set /a counter=2
set /p message=Message:
set "spam=%message%"
echo %spam%
if "%message%"=="%spam%" (
set /a counter=%counter% + 5
echo %counter%
)
pause
goto
start
You need to use delayedexpansion. %counter% is being evaluated outside of the If statement but not inside of it.
#echo off
setlocal enabledelayedexpansion
:start
set /a counter=2
set /p message=Message:
set "spam=%message%" echo %spam%
if "%message%"=="%spam%" ( set /a counter=%counter% + 5
echo !counter! )
pause
goto start
I've got a set of variables called p1 to p9
I've got 2 vars to set the range I'm interested in.
I want to list all the vars from minimum range +1 to the max.
It looks like that (and it won't work)
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo %p %currentvar% % //IT WON'T WORK AND I DON'T KNOW HOW TO MAKE IT WORK...
goto LOOP
) else (
goto END
)
:END
The result I'd like to see:
fff
ggg
hhh
iii
this might work for you:
#ECHO OFF &SETLOCAL
set /a currentvar=5
set /a maxvar=9
set /a RangeStart=currentvar+1
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result=maxvar-currentvar
echo Found %result% vars in the range.
for /l %%a in (%RangeStart% 1 %maxvar%) do call echo(%%p%%a%%
You need to Enabledelayedexpansion:
#echo off
Setlocal Enabledelayedexpansion
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo !p%currentvar%! &REM This is how you make a comment in batch
goto LOOP
) else (
goto END
)
:END
Endlocal
And that should do what you want to do. Also to make a comment use a new line and type :: or REM.
Mona
Sample for loop is given below. How to achieve same function without using for loop.
for /l %x in (1, 1, 100) do echo %x
:loop
set /a count+=1
echo %count%
if %count% neq 100 goto:loop
set /a a=1
:label
echo %a%
set /a a+=1
if %a%' EQU 100' (goto :outOfLoop)
goto :label
:outOfLoop
echo done!