I have a requirement where i'd like to read values from a .properties file
my properties file
From the above file I need to fetch
jaguar or anything after
Please help me. Thanks
echo off setlocal FOR /F "tokens=3,* delims=.=" %%G IN (test.properties) DO ( set %%G=%%H ) rem now use below vars if "%%G"=="file" set lfile=%%H if "%%G"=="path" set lpath=%%H if "%%G"=="extension" set lextention=%%H echo %path% endlocal
For /F "tokens=1* delims==" %%A IN (test.properties) DO ( IF "%%A"=="file" set file=%%B ) echo "%file%"
hope this could help
@echo off FOR /F "tokens=1,2 delims==" %%G IN (test.properties) DO (set %%G=%%H) echo %file% echo %extension% echo %path%
Note that there is no space after %%H. Else this causes a space to be appended, to file paths for example, and will cause file not found errors when the variables from the property files are used as part of a file path.Struggled for hours because of this!
A solution with support for comments (# style). See comments in code for explanation.
# some comment with = char, empty line below #invalid.property=1 some.property=2 some.property=3 # not sure if this is supported by .properties syntax text=asd=f
@echo off rem eol stops comments from being parsed rem otherwise split lines at the = char into two tokens for /F "eol=# delims== tokens=1,*" %%a in (test.properties) do ( rem proper lines have both a and b set rem if okay, assign property to some kind of namespace rem so some.property becomes test.some.property in batch-land if NOT "%%a"=="" if NOT "%%b"=="" set test.%%a=%%b ) rem debug namespace test. set test. rem do something useful with your vars rem cleanup namespace test. rem nul redirection stops error output if no test. var is set for /F "tokens=1 delims==" %%v in ('set test. 2^>nul') do ( set %%v= )
set test. (see above):
The most important parts are:
for-loop with the
if-checks that both variables
What you do in the
for-loop with the variable and its value is certainly up to you - assigning to some prefixed variables was just an example. The namespacing approach avoids that any other global variable gets overridden.
For example if you have something like
appdata defined in your .properties file.
I'm using this to get rid of an extra config.bat and instead using one .properties file for both the java app and some support batch files.
Works for me, but certainly not every edge case is covered here, so improvements welcome!