?
Solved

Batch file to read INI file value

Posted on 2010-01-12
11
Medium Priority
?
4,048 Views
Last Modified: 2012-06-27
Hi,

We have a batch file which is in turn compiled into an exe before usage.  We are changing the batch file so that it reads from an ini file to get the settings that it requires... (rather than recompiling the exe each time)...

I've had no problem setting this up so that it can read the ini file, but seem to have staggered across a problem if there are any spaces.  My code is as follows:

[**START CODE**]
Type %SDQINIFILE% | find %SDQ_Version% > %SDQQUERYFILE%
For /f "tokens=1-3 delims== " %%d in (%SDQQUERYFILE%) do set SDQ_Version=%%e
[**END CODE**]

In the above sample the variables are as follows:
%SDQINIFILE% is the location and name of the INI file it should read
%SDQ_Version% is the text it should look for, which in this case would be "SDQ_Version="
%SDQQUERYFILE% is the location and name of the temporary results file

This is fine, and would return the current version (i.e. v3.1) once the variable is called later in the script... However, some of the INI lines are going to require spaces in it... i.e. it's going to return a file location, or a name of something...

As there is no way to tell how many words/spaces there will be in the answer, I'm a bit stuck as to how to approach this one, as it won't follow the 3 items rule (NAME=VALUE)...

i.e. SDQ_MAINMENUFIX1_NAME=Reset MS-Office User Info

If we were to apply my code above, it would return a value of "Reset"...

any ideas?
0
Comment
Question by:Jofnn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26294292
Just remove the space after the = in the delims= entry in the for command.  Currently is split into tokens each space and equals sign delimtied part,

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26294307
You can also combine the reading of the file into the for command if you wish:

For /f "tokens=1-3 delims==" %%d in ('Type %SDQINIFILE% | find %SDQ_Version%') do set SDQ_Version=%%e

Open in new window

0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 26294322
or even
For /f "tokens=1-3 delims==" %%d in ('Type %SDQINIFILE% ^| find %SDQ_Version%') do set SDQ_Version=%%e

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:vikas_madhusudana
vikas_madhusudana earned 500 total points
ID: 26294351
For /f "tokens=1,2,*  delims== " %%d in (%SDQQUERYFILE%) do set SDQ_Version=%%e

can you try this
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26294419
That is another way of doing it.. but easier just to remove the delimiter -- why split it up at every space then re-join by saying to return all parameters?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26294444
Though actually that would assign %d = INI entry, %e = first word and %f = rest of line.  The entry would be "tokens=1* delims==" if you wanted to do it that way (this could be an issue if there is an = in the data I suppose).
0
 
LVL 57

Assisted Solution

by:Bill Prew
Bill Prew earned 200 total points
ID: 26294489
I actually do it just a little bit different in some BAT files I have.  I actually make the variable name in the BAT file match the setting name in the INI file (the part before the = sign.  With this approach I can do the following and get all the INI settings into BAT variables all at once.

REM Load settings from config file into variables with the setting namefor /F "tokens=1* delims=:" %%d in ('type "%~f1"') do set %%d=%%e
So an INI could look like:

Setting1=Value for setting 1Setting2=Value for setting 2

or

Setting1:Value for setting 1Setting2:Value for setting 2

and after that single FOR statement runs all variables are set in the BAT file, such as

%Setting1%%Setting2%
~bp
0
 
LVL 3

Author Comment

by:Jofnn
ID: 26296461
Hi guys,

thanks for your help on this... I'll try them when I'm back in the office in the morning.

Jofnn
0
 
LVL 3

Author Comment

by:Jofnn
ID: 26302354
Thanks very much to you all for your help... I've taken a bit from each of your examples, and have decided to go with the following
For /f "tokens=1* delims== " %%d in ('Type %SDQINIFILE% ^| find %SDQ_Version%') do set SDQ_Version=%%e

Open in new window

0
 
LVL 3

Author Closing Comment

by:Jofnn
ID: 31676152
I have merged the help from the experts here, and have given points according to this.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 26302672
Ok fair enough!  Lots of ways of ending up at the same goal so whatever is clearest for you.

Steve
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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