I am trying to iterate a string in batch script:
set var="1 2 3"
for /F %%i in (%var%) do (
echo %%i
)
and getting this output:
C:\>batch.bat
C:\>set var="1 2 3"
C:\>for /F %i in ("1 2 3") do (echo %i )
C:\>(echo 1 )
1
I expect that all 3 numbers will be printed:
1
2
3
What am I doing wrong?
That’s because FOR/F splits a each line into multiple tokens, but you need to define how many tokens you want to process.
EDIT: Other solutions
Like the answer of Ed harper:
You could also use a normal FOR-loop, with the limitation that it will also try to serach for files on the disk, and it have problems with
*and?.Or you use linefeed technic with the FOR/F loop, replacing your delim-character with a linefeed.
This works as the FOR/F sees five lines splitted by a linefeed instead of only one line.