Search code examples
windowsbatch-file

Windows Batch scripting(splitting of strings using multiple delimiters)


I have a property file (test.properties) which has a variable which holds multiple strings. Ex: var=str1;str2;str3;.....

I need to use the above properties file in my batch file (abc.bat), so that i could print the values line by line. Output of the batch file should look like this... str1 str2 str3 ... ... (and so on)

Any help could be appreciated..Thanx:)

Ive tried to use "for loop" to seperate the values from first delimiter(=) in this way...

IF EXIST "test.properties"
(
    ECHO test.properties file found
    for /F "tokens=1,2 delims==" %%A IN (test.properties) DO
    (
        set value="%%B"
        ECHO !value!
    ) 
)
Output=str1;str2;str3;....

Now if i want to parse the strings in "!value!" line by line i use ...

for /F "tokens=* delims=;" %%x IN ("!value!") DO
(
    ECHO %%x
)

I am facing error.....Any help?


Solution

  • just use a plain for to get elements of a list (; is a standard delimiter)

    @echo off
    setlocal enabledelayedexpansion
    
    >test.properties echo var=str1;str2;str3;str4;str5;str6
    
    IF EXIST "test.properties" (
        ECHO test.properties file found
        for /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
            set "value=%%B"
            ECHO !value!
        ) 
        for %%x IN (!value!) DO echo %%x
    )