Your code has nothing wrong with logic, it’s a syntax error and the lack of use of ENABLEDELAYEDEXPANSION
to the for
with set
, in the syntax error you broke a line after the do
of your for
that couldn’t have broken, you did it:
for /l %%a in (1,1, %number_of_agents%) do
(
When should this be:
for /l %%a in (1,1, %number_of_agents%) do (
If you use the (
in the next line cmd will not recognize as sequence of the command, because in CMD the parentheses are the way to pass the instruction as a block, but the instruction needs to be part of the for
and not being on the same line will not understand as such, after all BAT is not quite a "programming language" as "other", but rather a sequence of commands that accepts some syntaxes, see what happens with your script:
C:\>C:\Users\usuario\Desktop\a.bat
C:\>set number_of_agents=2
A sintaxe do comando está incorreta.
C:\>for /l %a in (1,1, 2) do
C:\>
Noticed the message A sintaxe do comando está incorreta.
? Now with the block (the parentheses) starting on the same line:
C:\>C:\Users\new_g\Desktop\a.bat
C:\>set number_of_agents=2
C:\>for /L %a in (1 1 2) do (echo %a )
C:\>(echo 1 )
1
C:\>(echo 2 )
2
C:\>pause
Using set
within a for
To use the set
within the is necessary to enable the ENABLEDELAYEDEXPANSION
, see an example without:
@echo off
set TESTE=0
for %%v in (1 2 3) do (
set /p TESTE=Digite algo:
echo Resposta: %TESTE%
)
pause
It will always emit "Answer: 0" (zero, which is the initial value), then doing this (change the signals %%
by exclamations), thus:
@echo off
setlocal EnableDelayedExpansion
set COUNT=0
for %%v in (1 2 3 4) do (
set /p TESTE=Digite algo:
echo Resposta: !TESTE!
)
pause
And in the end it can disable with SETLOCAL DisableDelayedExpansion
as explained in: https://ss64.com/nt/delayedexpansion.html