0

I have a properties file that contain values in the format of

variable_one = value_one
variable_two = value_two
.
.
.
.



. and so on
with file name as testing.properties which is in the same directory of batch script No i am building a batch file that should run in such a way that double clicking it has to read values into some variables and display it on screen

For /F "tokens=1* delims==" "variable_one", "variable_two" IN (testing.properties) DO (
	IF "variable_one"=="variable_one" set local_variable_one=%%B
  IF "variable_two"=="variable_two" set local_variable_two=%%C
	)
	
echo "%local_variable%"

pause

2
  • Do you want to read all the properties and display their values, or read certain properties and display their values? Commented Jan 11, 2019 at 5:58
  • 1
    The FOR loop must be as follows with the ugly spaces around equal sign in properties file which must be additionally removed: for /F "usebackq tokens=1* delims== " %%I in ("testing.properties") do set "%%I=%%J". No variable name (line) should start with ; because of for ignores those lines. On no spaces around equal sign the FOR loop would be: for /F "usebackq delims= eol=" %%I in ("testing.properties") do set "%%I". See also Why is no string output with 'echo %var%' after using 'set var = text' on command line? Commented Jan 11, 2019 at 5:59

1 Answer 1

1

Does the properies file really contain the spaces between the = ?

Something like this will read all the properies and display their values only:

@echo off

for /f "tokens=2 delims== " %%i in (testing.properties) do echo %%i
pause

where this will display the property name as well as the value:

@echo off

for /f "tokens=1,* delims== " %%i in (testing.properties) do echo Property: %%i Value: %%j
pause

if you really want to set the properties as variables (to use as readable variables).

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,* delims== " %%i in (testing.properties) do (
    set property=%%i
    set value=%%j
    echo !property! !value!
)
pause

Comparing values and then setting them is another task on its own. Here you are giving an example, so give a real example and we can work from there. For instance. We can set the property name value as the vaiable name without manually typing it:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,* delims== " %%i in (testing.properties) do (
    set %%i=%%j
    echo Property: %%i has Value: !%%i!
)
pause

or even add a something to the string:

@echo off
setlocal enabledelayedexpansion
for /f "tokens=1,* delims== " %%i in (testing.properties) do (
    set local_%%i=%%j
    echo Property: %%i has value !local_%%i! set as Variable local_%%i
)
pause

Finally, giving your variables unique names will depend on some search criteria, you can probably use identifiers from your properties. For instance do a findstr on part of your property, or simply matching a numeric value or anything unique in a property.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.