I want to read a property file like below and get the particular key value
My properties file looks like
name=Chenjo.net version=1.0.2 date=24/March/2010
I want to get the version from the properties file. You can do this by various methods.
Using Type function:
C:\Users\Chenjo\Desktop>type test.properties | find "version"
There is a disadvantage with this method you could not store that value in a variable.
Using For Loop:
C:\Users\Chenjo\Desktop>FOR /F %i IN (test.properties) DO echo %i
Using this command you can read that file line by line.
FOR /F "eol=; tokens=2,2 delims==" %i IN (test.properties) DO echo %i
Using this commend you can get the values only.
eol is End of Line
tokens is specifying which tokens are displayed – 2,2 means only the second token will be displayed
delims is the deliminator. This is the separator
FOR /F "eol=; tokens=2,2 delims==" %i IN ('findstr /i "version" test.properties') DO set version=%i
Using findstr we get the correct string from the properties file and give as an input to the for loop.
That for loop processes the result and set that value to the variable version.
findstr /i means it is not a case sensitive one.
Using echo you can get the value.
When using it in a bat, add a % befor %i. That is like
FOR /F "eol=; tokens=2,2 delims==" %%i IN ('findstr /i "version" test.properties') DO set version=%%i echo %version%