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?
enthuguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITCommented:
Not sure what you are trying to do. Please clarify. Are you trying to extract somevalue from file1 or file2?

If so, this returns the matching line.

Make a file Prop1.txt:
someprop1=somevalue1
someprop2=somevalue2
someprop3=somevalue3
someprop20=somevalue20

Open in new window


Make a test.bat file of this code:
@echo off
setlocal EnableDelayedExpansion

set Arg=%1
if [%Arg%] equ [] echo Missing someprop? argument& exit /b
set fnusers=Prop1.txt
for /f "tokens=1-2* delims==" %%a in ('type "%fnusers%"') do (
	if /i [%Arg%] equ [%%a] echo %Arg%=%%b&exit /b 0
)
exit /b 1
pause

Open in new window


Open a CMD window in the same folder as test.bat
Type:
test.bat someprop3

Open in new window


Result:
someprop3=somevalue3

Open in new window

0
enthuguyAuthor Commented:
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
0
NVITCommented:
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
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

enthuguyAuthor Commented:
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

0
NVITCommented:
Inside import.bat, prefix ÇALL on each line...

call test.bat gitrepo
...

Open in new window

0
enthuguyAuthor Commented:
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
0
enthuguyAuthor Commented:
Hi NVIT,
could you advise me pls

I tried many options e.g /b 3, return value. etc. but no luck so far :(
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Do you need the merged property file somewhere? Or do you just want to have both files' property values read into variables?
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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.
1
enthuguyAuthor Commented:
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

0
enthuguyAuthor Commented:
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. :)
0
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
That can get pretty complex. You need to make sure the referred property is already read in to allow for a single-pass evaluation, and checking in the script requires proper parsing and some more logic; if we need that, switching to PowerShell is my recommendation - that offers a lot of features we would have to make use of.

Assuming we can go with a single pass:
@echo off
for /F "delims=" %%L in (C:\Temp\EE\file1.properties) do call set "%%L"

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.