Link to home
Start Free TrialLog in
Avatar of enthuguy
enthuguyFlag for Australia

asked on

How to source a property file inside windows batch file and use those property value

HI Experts

Could you advise what is the best way to source two property files inside windows batch file and use those property value in the batch script.

Scenario:
1. I have around 20 property/value in a file1.properties and file2.properties files. Assume file1 and file2 has 10 each...coming from two different source (Hope I can concat/append into one single file)
someprop1=somevalue1
someprop2=somevalue2
someprop3=somevalue3
.
.
someprop20=somevalue20

Open in new window


2. Have few batch files, where I would like to source above file1.properties & file2.properties (or merged single file) files inside these batch files and use those property value where ever required.
3. I'm used to to Linux way of source <file1.properties>. But looks like this is not possible in Window Batch? :)
4. In case file1.properties was populated with additional property/value dynamically. Instead of 10...it can have 15 in the future.

Could you help, what is the best way to achieve this please?
SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of enthuguy

ASKER

Hi NVIT,
thanks so much for your quick response. but sorry, I wasn't clear enough

Would like to highlight few things
1. Property name could be anything. E.g
gitrepo=1.2
gitbranch=release_1.2
app_home=c:\application\home
scriptdir=c:\scripts 
staging_dir=c:\staging

Open in new window



2. thinking, inside my batch script. I can use %gitrepo% or %staging_dir% to read respective values
Ok. I think it basically works the same. E.g.
test.bat gitrepo

Open in new window


Returns
gitrepo=1.2

Open in new window


If you want to return instead
1.2

Open in new window


Then revise the line to
if /i [%Arg%] equ [%%a] echo %%b&exit /b 0

Open in new window


I hope that helps
Thanks again,

so Inside main script. e.g import.bat....for every property value that I need,
I need to have a line test.bat gitrepo to get 1.2?

e.g
test.bat gitrepo
test.bat gitbranch
test.bat app_home
test.bat scriptdir
test.bat scriptdir

Open in new window

Inside import.bat, prefix ÇALL on each line...

call test.bat gitrepo
...

Open in new window

Hi NVIT,
Thanks it works fine.

Two quick help pls :)
1. I replaced the line wth "if /i [%Arg%] equ [%%a] echo %%b&exit /b 0"
but still it return gitrepo=1.2

2. how to assign this return value to a variable, so I can use in few locations in the script. e.g gitrepo=call test.bat gitrepo
Hi NVIT,
could you advise me pls

I tried many options e.g /b 3, return value. etc. but no luck so far :(
Do you need the merged property file somewhere? Or do you just want to have both files' property values read into variables?
Simple in-memory merge and setting of all properties found as variables:
@echo off
for /F "delims=" %%L in (C:\Temp\EE\file1.properties) do set %%L

Open in new window

The property files must consist of property=value pairs only for this to work. After running this "import", you can use %gitrepo% etc.
Hi Qlemo,
thanks in advance.

1. I can concat two or more property files like below. Believe below approach is not bad.
copy Prop1.txt + Prop2.txt contact.txt

Open in new window


2. Yes, would like to read all the property values into few variables. so I can use inside my script in many location. Eg. 5 values into 15 variables

3. After minor change in the echo I can get only values now.

From
if /i [%Arg%] equ [%%a] echo %Arg%=%%b&exit /b 0

Open in new window


To
if /i [%Arg%] equ [%%a] echo %%b&exit /b 0

Open in new window

Hi Qlemo,
Since I'm not an expert. Your couple of lines solution looks simple and helped me achieve what I was texpecting

Only one small enhancement, I tried but didnt help. I did in my property file, but output is not as expected, may be that is not the approach I was taking.

someprop1=somevalue1
app_base_dir=c:\app
app_bin=%app_base_dir%\bin

Open in new window


When I echo, I get below. Looks like it is not liking the way I did variable assignment (reuse variable)
c:\app
%app_base_dir%\bin

Open in new window



this is nice to have, so if you have a solution...that would really help. otherwise I can survive without that. :)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial