for-loopbatch-file

Using FOR command, attempting to parse a comma-delimited list embedded with blanks


I am attempting to use FOR to parse a list delimited with commas and embedded with blanks.

Example:

SET "X=My Town,Four square,a b c d"

I know

FOR "delims=," %%Q IN (%X%) DO ECHO '%%Q' 

doesn't work, but it indicates my intent. I want three lines produced

My Town
Four square
a b c d

Solution

  • How about

    @ECHO OFF
    SETLOCAL
    SET "X=My Town,Four square,a b c d"
    
    FOR %%Q IN ("%X:,=","%") DO ECHO %%~Q
    
    GOTO :EOF
    

    Works for me!