bat 读配置文件 *.properties
bat 读配置文件 *.properties
圣洁之子 发表于2年前
bat 读配置文件 *.properties
  • 发表于 2年前
  • 阅读 23
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

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. 

echo %version% 

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%   

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 6
博文 219
码字总数 82224
×
圣洁之子
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: