How to extract name/value pair from config file from DOS batch script?

Hello World,

I've been working on a DOS batch script that needs to get the name of a log file from a configuration file.

The config file is set up as a name/value pair file as follows:

prog.varone=prog'svalue1
prog.vartwo=prog'svalue2
sys.varone=sysval1

Now I can find the right parts using this command:

> find "prog.varone" myconfigfile.cfg

the output of this looks somthing like this:

---------- MYCONFIGFILE.CFG
prog.varone=prog'svalue1

And I know I can extract only the second line of this output using something like this:

> for /F "skip=2 delims=" %%a in (%~fs1) do echo %%a

But I'm not sure how to tie it all together and strip off the left side of the "="... I would ultimately like to have this:

> echo The value of the configuration is: %workingvariable%

to output:

> The value of the configuration is: prog'svalue1

Any ideas?
bganoushAsked:
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.

Farhan KaziSystems EngineerCommented:
Copy and paste following code into notepad and save it with any name having .cmd extension.

Like:
Then Click Start -> Run -> Cmd.exe -> OK
FileFind.cmd "prog.varone" myconfigfile.cfg

Hope this helps!
Farhan
@ECHO OFF
SETLOCAL
SET FindStr=%1
SET FileName=%2
IF NOT EXIST "%FileName%" ECHO "%FileName%" file not found. &&EXIT /B 0
FOR /F "tokens=2 delims==" %%v IN ('FIND /I %FindStr% "%FileName%" ^|FIND "="') DO (
	ECHO The value of the configuration is: %%v)
EXIT /B 0

Open in new window

0

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

It didn't work right away, I had to add quotes around "%FindStr%"...  Without the quotes, I get a messages saying that the "%FindStr%" is not found: (Strange)

File not found - PROG.VARONE

The only problem now is that if I have two or three variables with almost the same name like:

prog.varone=outputfile.log
prog.varone.sublog=outputfile2.log

then the script pulls out all the variables, not only the one I'm interested in.
0
oBdACommented:
Try this; the dirty work is in a subroutine so that you can call it easily:

@echo off
setlocal
set ConfigFile=myconfigfile.cfg
:: *** Call the subroutine; 
:: *** first argument: the name of the var in the config file to look for
:: *** second argument: the batch variable that should have the value assigned.
call :ReadConfig prog.varone VarOne
echo Value for prog.varone: %VarOne%
call :ReadConfig prog.varone VarTwo
echo Value for prog.varone.sublog: %VarTwo%
REM other parts of your script follow here ...
 
:: *** Subroutines start here; no changes below this line
goto :eof
:ReadConfig
for /f "tokens=2 delims==" %%a in ('type "%ConfigFile%" ^| find /i "%~1="') do set %2=%%a
goto :eof

Open in new window

0
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
Microsoft DOS

From novice to tech pro — start learning today.