Batch Script - Call a subroutine for each token

11,609

for /f is really meant to parse text files or commands, and parses them one whole line at a time. Look at what this produces:

SET devices=host1 host2 host3
FOR /F "tokens=*" %%G IN ("%devices%") DO call :sendReq %%G %%H %%I
goto :EOF

:sendReq
echo Parm1: %1
echo Parm2: %2
echo Parm3: %3
GOTO :EOF

(i.e. each go through the loop reads one whole line, and assigns the different tokens to %G, then %H, %I, %J, ... depending on how many tokens you actually have.)

You just need the simpler version of for:

SET devices=host1 host2 host3
FOR %%G IN (%devices%) DO call :sendReq %%G
goto :EOF

:sendReq
echo In sendReq: %1
GOTO :EOF
Share:
11,609
liv2luv
Author by

liv2luv

Updated on June 15, 2022

Comments

  • liv2luv
    liv2luv almost 2 years

    I am trying to pass a token to a subroutine in batch script-

    SET devices=host1 host2 host3
    FOR /F "tokens=* delims=," %%G IN (%devices%) DO CALL :sendReq %%G
    
    :sendReq
    
    curl.exe http://%1:1234/service/monitor
    
    GOTO :EOF
    

    Problems - The system cannot find the file host1 - is the error message not sure why.

    %1 is not resolving to host1?