1

I have a properties file separated using ; which needs to be read using a batch script and the properties will be set as environment variables.

All the properties need not be on separate lines eg:

PORT=9099;
URL=www.google.com;
DOMAIN=D1;USER_NAME=ADMIN;PASSWORD=PASS;
LOG_LEVEL=FINE;

Here the third line has multiple properties on the same line.

How do I parse this ? I tried the below but it does not split after the first ; for the third line

FOR /f "tokens=1,2 delims=;" %%a in (%parameterFile%) do (
echo a is %%a
echo b is %%b
FOR /F "tokens=1,2 delims==" %%G IN (%%b) DO (set %%G=%%H)  
)
Anand
  • 47
  • 2
  • 7

1 Answers1

0

This batch code could be used for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not defined ParameterFile set "ParameterFile=%TEMP%\%~n0.tmp"
set "DeleteParameterFile="
if exist "%ParameterFile%" goto ParseParameterFile
set "DeleteParameterFile=1"
(
    echo PORT=9099;
    echo URL=www.google.com;
    echo DOMAIN=D1;USER_NAME=ADMIN;PASSWORD=PASS;
    echo LOG_LEVEL=FINE;
)>"%ParameterFile%"


:ParseParameterFile
for /F usebackq^ delims^=^ eol^= %%A in ("%ParameterFile%") do (
    set "Variable="
    for %%B in (%%A) do (
        if not defined Variable (
            set "Variable=%%B"
        ) else (
            call set "%%Variable%%=%%B"
            set "Variable="
        )
    )
)

rem Add more code here using the environment variables set from file.

if defined DeleteParameterFile del "%ParameterFile%"
endlocal

The outer FOR with option /F parses the text file as defined by environment variable ParameterFile specified in double quotes requiring usage of option usebackq to be interpreted as name of a text file line by line with always ignoring empty lines. FOR ignores by default lines starting with a semicolon because of eol=; is the default for end of line option. FOR splits up by default also the current line into substrings using normal space and horizontal tab character as delimiters and assigns only first space/tab separating string to specified loop variable.

Line splitting behavior and ignoring lines starting with a semicolon is not wanted here. For that reason delims= defines an empty list of delimiters disabling line splitting behavior and eol= defines no end of line character resulting in ignoring just empty lines. The options cannot be specified in this case in a double quoted argument string. For that reason the three options must be specified in an argument string on which all spaces and equal signs must be escaped with caret character ^ to be interpreted as literal characters and not as argument string delimiters.

FOR interprets horizontal tab, normal space, no-break space (with OEM code page code value 255 decimal), equal sign, comma and semicolon on not being inside a double quoted string as delimiters of a set of strings to process in a loop on not using option /F. So for the given example it is possible to parse a line from the file using one more FOR loop.

Please read How does the Windows Command Interpreter (CMD.EXE) parse scripts? An environment variable defined/modified within a command block cannot be referenced in same command block with using the syntax %variable% because of cmd.exe replaces all %variable% by current value of variable already on parsing entire command block starting with ( and ending with matching ) which means in this case before execution of outer FOR command. The workarounds are using delayed environment variable expansion or using a subroutine or using command CALL and syntax %%variable%%.

The line call set "%%Variable%%=%%B" is parsed to call set "%Variable%=%B" before execution of outer FOR. This line is parsed a second time during execution of inner FOR because of command CALL replacing %Variable% by current value of environment variable Variable before execution of command SET. So the value assigned to loop variable B on every even iteration of the inner FOR loop is assigned to the environment variable with name determined on every odd iteration of the inner FOR loop and assigned to environment variable with name Variable.

Please note that a value like the user name string containing a character being interpreted as delimiter by inner FOR requires that this string value is enclosed in " or this batch file solution produces a wrong result.

The environment variables defined in inner FOR loop are available up to the command ENDLOCAL which restores the environment (environment variables, current directory, state of command extensions and state of delayed expansion) before execution of this batch file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143