[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

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

Posted on 2008-02-08
3
Medium Priority
?
1,022 Views
Last Modified: 2010-07-27
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?
0
Comment
Question by:bganoush
3 Comments
 
LVL 26

Accepted Solution

by:
Farhan Kazi earned 2000 total points
ID: 20852695
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
 

Author Comment

by:bganoush
ID: 20853042

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
 
LVL 86

Expert Comment

by:oBdA
ID: 20853656
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
The video provides a quick and easy steps to migrate MBOX file to well known Outlook PST and Office 365. Besides this, it also supports and migrates more than 20 email clients of MBOX which include AppleMail, Opera, Thunderbird and SeaMonkey effortl…
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question