• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4503
  • Last Modified:

Batch file to read INI file value

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
Jofnn
Asked:
Jofnn
3 Solutions
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
vikas_madhusudanaCommented:
For /f "tokens=1,2,*  delims== " %%d in (%SDQQUERYFILE%) do set SDQ_Version=%%e

can you try this
0
 
Steve KnightIT ConsultancyCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
Bill PrewCommented:
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
 
JofnnAuthor Commented:
Hi guys,

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

Jofnn
0
 
JofnnAuthor Commented:
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
 
JofnnAuthor Commented:
I have merged the help from the experts here, and have given points according to this.
0
 
Steve KnightIT ConsultancyCommented:
Ok fair enough!  Lots of ways of ending up at the same goal so whatever is clearest for you.

Steve
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now