Find if parameter contains a comma separator, if it does, split it

I need to process several parameters in a batch file. It might be a simple parameter or a composite one with an argument and related path.

Format for a composite parameter should be something like: ""ARG=CUSTOM_SOURCE,SOURCE=C:\Folder\OtherFolder\SourceFiles""

I can't figure out how to get and set values for ARG and SOURCE.

What I've got so far:

setlocal enableextensions enabledelayedexpansion     

set sep=","
set str1=%3

if not "x!str1:%sep%=!"=="x%str1%" (
    echo found pattern   rem gets here with no problem
    echo 3 is %3   rem output: string is "ARG=CUSTOM_SOURCE,SOURCE=C:\Folder\OtherFolder\SourceFiles"
    set sourceString=%3 
    echo "%sourceString%" rem output: ""

    REM extract source path from argument
    :nextVar
    for /F "tokens=1* delims=," %%a in ("%3") do (                     
        set %%a
        echo "%%a" REM output: "ARG CUSTOM_SOURCE SOURCE C:\Folder\OtherFolder\SourceFiles"
        set sourceString=%%b 
        echo "%%b" "sourceString" REM output: "" ""         
    )
    if defined sourceString goto nextVar
    echo "%ARGS%" "%SOURCE%" REM output: "" "" 
)  

Code for splitting source string I got from this answer: https://stackoverflow.com/a/18821153/6385702.

Tried other option too but nothing works so far. Seems, the main problem I have with set var=string part. Even with enabledelayedexpansion I get empty values.

I'm not fluent with batch files and got really stacked with this one. What am I missing here?

EDIT: parameters are passed from hta application (and also can be passed from command prompt directly) format is following: cmd /k myBatch.cmd PARAM REQ_PARAM "ARG=CUSTOM_SOURCE,SOURCE=C:\Folder\OtherFolder\SourceFiles" OPTIONAL_PARAM

1 answer

  • answered 2017-10-11 10:19 Jane Doe

    The reason for failing was the double doublequotes in the passed parameter. After removing them, the arguments are processed correctly:

    set test=%3
    echo "%test%" rem output: ""ARG=CUSTOM_SOURCE,SOURCE=C:\Folder\OtherFolder\SourceFiles""
    
    set test=%test:"=%
    echo "%test%" rem output: "ARG=CUSTOM_SOURCE,SOURCE=C:\Folder\OtherFolder\SourceFiles"
    
    :nextVar
       for /F "tokens=1* delims=," %%a in ("%test%") do (
          set %%a
          set test=%%b
       )
    if defined test goto nextVar
    echo ARG=%ARG%, SOURCE=%SOURCE%
    rem output: ARG=CUSTOM_SOURCE, SOURCE=C:\Folder\OtherFolder\SourceFiles