I want to parse through a single line of text where the words are separated by a space and store each word in its own array simulating variable. According to ss64 I should be able to do this. I may not be understanding delims or tokens because my code works for 1 iteration and stops.
::@echo off
setlocal enabledelayedexpansion
set string=foo bar baz
set /a index=0
for /F "tokens=* delims= " %%i in ("foo bar baz") do (
echo %%i
echo !index!
set words[%index%]=%%i
set /a index+=1
echo !index!
)
set words[
Here is the output I get with debug echoes:
C:\Users\isimm\Desktop\batintercept>stringparse.cmd
C:\Users\isimm\Desktop\batintercept>setlocal enabledelayedexpansion
C:\Users\isimm\Desktop\batintercept>set string=foo bar baz
C:\Users\isimm\Desktop\batintercept>set /a index=0
C:\Users\isimm\Desktop\batintercept>for /F "tokens=* delims= " %i in ("foo bar baz") do (
echo %i
echo !index!
set words[0]=%i
set /a index+=1
echo !index!
)
C:\Users\isimm\Desktop\batintercept>(
echo foo bar baz
echo !index!
set words[0]=foo bar baz
set /a index+=1
echo !index!
)
foo bar baz
0
1
C:\Users\isimm\Desktop\batintercept>set words[
words[0]=foo bar baz
tokens=* gives me the above output, tokens=1,2,3 gives me only the first word, tokens=1 or tokens=2 or tokens=3 gives me the 1st, 2nd, or 3rd word. As I understand it "delims= " is redundant because by default spaces are used but it's the same whether I include delims or not.
I also tried using underscores in case there is some bug with spaces and also tried putting the text in a foo.txt file instead of a literal string. In both cases I get the same result as with the original string.
The output I'm expecting is:
words[0]=foo
words[1]=bar
words[2]=baz