enthuguy
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)
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?
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ok. I think it basically works the same. E.g.
Returns
If you want to return instead
Then revise the line to
I hope that helps
test.bat gitrepo
Returns
gitrepo=1.2
If you want to return instead
1.2
Then revise the line to
if /i [%Arg%] equ [%%a] echo %%b&exit /b 0
I hope that helps
ASKER
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?
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
Inside import.bat, prefix ÇALL on each line...
call test.bat gitrepo
...
ASKER
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
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
ASKER
Hi NVIT,
could you advise me pls
I tried many options e.g /b 3, return value. etc. but no luck so far :(
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
The property files must consist of property=value pairs only for this to work. After running this "import", you can use %gitrepo% etc.
ASKER
Hi Qlemo,
thanks in advance.
1. I can concat two or more property files like below. Believe below approach is not bad.
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
To
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
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
To
if /i [%Arg%] equ [%%a] echo %%b&exit /b 0
ASKER
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.
When I echo, I get below. Looks like it is not liking the way I did variable assignment (reuse variable)
this is nice to have, so if you have a solution...that would really help. otherwise I can survive without that. :)
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
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
this is nice to have, so if you have a solution...that would really help. otherwise I can survive without that. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Open in new window
2. thinking, inside my batch script. I can use %gitrepo% or %staging_dir% to read respective values