Parsing a comma-delimited string

  • Thread starter Thread starter Rich Pasco
  • Start date Start date
R

Rich Pasco

In a BAT file, I would like to use a FOR statement to parse a comma-
delimited string.

For example suppose string s has value "a,b,c" then I need the FOR
statement to iterate "echo a" and "echo b" and "echo c" in order.

This doesn't work as intended:

for /f "delims=," %%v in ("a,b,c") do echo %%v

It only does "echo a" but not the rest.

I tried adding "tokens=*" but that still doesn't iterate. It puts
b and c into %%w and %%x instead of subsequent iterations.

Please help.

- Rich
 
In a BAT file, I would like to use a FOR statement to parse a comma-
delimited string.

For example suppose string s has value "a,b,c" then I need the FOR
statement to iterate "echo a" and "echo b" and "echo c" in order.

This doesn't work as intended:

for /f "delims=," %%v in ("a,b,c") do echo %%v

It only does "echo a" but not the rest.

I tried adding "tokens=*" but that still doesn't iterate. It puts
b and c into %%w and %%x instead of subsequent iterations.

Try this:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF
SET S="a,b,c"

:: Remove the "quotes" (or don't use them in the first place)
SET S=%S:"=%

:: Then use a plain FOR IN DO statement
FOR %%V IN (%S%) DO ECHO %%V

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects
 
William said:
:: Then use a plain FOR IN DO statement
FOR %%V IN (%S%) DO ECHO %%V

Thank you. While

for %%v in (a,b,c) do echo %%v

does work with simply a,b,c, it fails when the expressions are more
complicated than single letters. For example,

for %%v in (Joe Blow <[email protected]>, Steve <[email protected]>) do echo %%v

should (but does not) print

Joe Blow <[email protected]>
Steve <[email protected]>

Can you suggest how to make that one work?

- Rich
 
Thank you. While

for %%v in (a,b,c) do echo %%v

does work with simply a,b,c, it fails when the expressions are more
complicated than single letters. For example,

for %%v in (Joe Blow <[email protected]>, Steve <[email protected]>) do echo %%v

should (but does not) print

Joe Blow <[email protected]>
Steve <[email protected]>

Can you suggest how to make that one work?

Try this recursive solution (note that variable String is destroyed
in the recursion, so save it in another variable if you need it later).
Note also that lead [Space]s in front of the second and subsequent
listemes (as in your example, but not mine) would be part of the
ECHOed substrings.

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF

SET String="One <[email protected]>,Two <[email protected]>,Three <[email protected]>"

:LOOP
FOR /F "tokens=1,* delims=," %%F IN (%String%) DO (
ECHO %%F
SET STRING="%%G"
GOTO LOOP
)

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects

Screen capture:

Lines that don't begin with two spaces have wrapped accidentally
====Begin cut-and-paste (omit this line)
@ECHO OFF

SET String="One <[email protected]>,Two <[email protected]>,Three <[email protected]>"

:LOOP
FOR /F "tokens=1,* delims=," %%F IN (%String%) DO (
ECHO %%F
SET STRING="%%G"
GOTO LOOP
)

====End cut-and-paste (omit this line)
Simulated Win2000 for study/demo use. Cut-and-paste as Batch text file.
Batch file troubleshooting: http://www.allenware.com/find?UsualSuspects


============Screen capture Windows 2000 simulated in Win95
C:\WORK>test
One <[email protected]>
Two <[email protected]>
Three <[email protected]>

C:\WORK>
============End screen capture
 
in message
....snip
FOR /F "tokens=1,* delims=," %%F IN (%String%) DO (

Sorry, message was slightly garbled owing to excessive
cut-and-paste, and in that line there's a spurious [Comma]
in the 1,*

The line should have read:

FOR /F "tokens=1* delims=," %%F IN (%String%) DO (

(however, it seems not to make any difference here)
 
William said:
Try this recursive solution (note that variable String is destroyed
in the recursion, so save it in another variable if you need it later).
[snip]

Thanks, that does seem to work!

- Rich
 
Back
Top