Akhilesh
Akhilesh

Reputation: 3

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?

Upvotes: 0

Views: 2380

Answers (2)

Shane
Shane

Reputation: 98

The for /F "tokens=* delims=;" %%x IN ("!value!") command does not do what you are hoping it will do.

It will NOT split the string in the value variable at each semi-colon delimiter delim=;, and execute the loop body with %%x set to each of the resulting substrings.

The for /F command will iterate once per line in a file and extract 1 or more values from that line. It runs the code in the loop body once for each line in the file.

When you use for /F on a quoted string, it treats that string like a single line in a file. It will extract 1 or more values from that string, but it will run the code in the loop body only once.

Here is a Batch script that works

@SetLocal EnableExtensions
@echo off

@rem Default Values Go Here
set sDEFAULT_PROP_FILE_PATH=%temp%\test.properties

@rem Get the path to the properties file from the command line
set sPropFilePath=%~1

@rem If the caller did not specify a properties file, assume the default;
if "%sPropFilePath%" EQU "" (
    @echo INFO: Using default properties file:  "%sDEFAULT_PROP_FILE_PATH%"
    set sPropFilePath=%sDEFAULT_PROP_FILE_PATH%
)

@rem If the properties file does not exist then give up.
if not exist "%sPropFilePath%" (
    @echo ERROR: properties file not found:  "%sPropFilePath%"
    goto :EOF
)

@rem Extract the property name and value.
for /F "tokens=1,2 delims==" %%A in (%sPropFilePath%) do (
    set sPropertyName=%%A
    set sPropertyValue=%%B
)

@rem Show property name and value extracted
@echo Property File: %sPropFilePath% contains:
@echo %sPropertyName% = %sPropertyValue%
@echo ----

@rem Repeatedly extract string before first delimiter
set sRemainder=%sPropertyValue%;
:LoopBegin
    for /F "tokens=1,* delims=;" %%A IN ("%sRemainder%") DO (
        set sExtracted=%%A
        set sRemainder=%%B
    )
    @echo.%sExtracted%
    if "%sRemainder%" NEQ "" goto :LoopBegin
:LoopEnd

@echo ----

The script works by repeatedly extracting two values from remaining property string: the string to display, and the next remaining property string.

To start sRemainder is set to the full property value "str1;str2;str3,...;strN".

The two values extracted are:

  1. sExtracted: substring before first semi-colon: "str1"
  2. sRemainder: everything after first semi-colon: "str2;str3;...;strN"

The script loops back around but now sRemainder is "str2;str3;...;strN", so the two values extracted are:

  1. sExtracted: substring before first semi-colon: "str2"
  2. sRemainder: everything after first semi-colon: "str3;...;strN"

Upvotes: 0

Stephan
Stephan

Reputation: 56180

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
)

Upvotes: 1

Related Questions