您的位置:首页 > 运维架构

bat读配置文件*.properties

2015-11-25 14:14 295 查看
原文地址:http://michael-paul.iteye.com/blog/740000

i want to read a below property file and get the particular key value

My properties file look like

name=arulraj.net
version=1.0.2
date=24/March/2010


I want get the version from the properties file. you can do this by various method.

Using Type function:

C:\Users\Arul\Desktop>type test.properties | find “version”

There is disadvantage with this method you could not store that value in a variable.

Using For Loop:

C:\Users\Arul\Desktop>FOR /F %i IN (test.properties) DO echo %i

using this command you can read that file by 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 specify the 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 get the correct string from the properties file and give as a input to the for loop. That for loop process the result and set that value to the variable version.

findstr /i means is not a case sensitive one

using echo you can get the value.

echo %version%

When you using in a bat add a % befor %i. That is Look like

FOR /F "eol=; tokens=2,2 delims==" %%i IN ('findstr /i "version" test.properties') DO set version=%%i
echo %version%
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息