Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Reading INI file in VBScript

2,013 views
Skip to first unread message

Ansu

unread,
Apr 10, 2008, 2:27:50 AM4/10/08
to
Hello everybody,

I am trying to read an MS Window ini file exaple of INI file, ad.ini

///////////////////////////////////////////////////////////////////////////////
[NETGctlintf]
TITLE=Skill Builder Courseware
TitlePrefix=Lesson:

[MMETest]
Device=YES

[mmtest]
device=installed
///////////////////////////////////////////////////////////////////////////////

Now I want to fetch the value "device = installed"

I tried with FileSystemObject. but it is not working. Can anybody help
me in this regard.

Thanks in Advance,
AD

Pegasus (MVP)

unread,
Apr 10, 2008, 4:15:02 AM4/10/08
to

"Ansu" <ansuma...@gmail.com> wrote in message
news:3844526a-169c-4bea...@n1g2000prb.googlegroups.com...

Let's have a look at what you've go so far!


Ansu

unread,
Apr 10, 2008, 8:10:58 AM4/10/08
to
On Apr 10, 1:15 pm, "Pegasus \(MVP\)" <I....@fly.com.oz> wrote:
> "Ansu" <ansuman.d...@gmail.com> wrote in message

Hi,
I am successful to write the script for my query. But I can not able
to make it generic. I am hard coding the value in the script. But I
want make it generic so that I can fetch any value,
this is my script,

////////////////////////////////////////////////////////////////////////////////////
Call FetchValue


Private Function FetchValue
Dim rc
Dim intPos

rc=ReadINI
intPos=Instr(rc,"=")
Reporter.ReportEvent micDone, "Status is: " & " " & mid(rc,intPos
+1),"Done"
End Function

Private Function ReadINI
Dim rline
Dim objFso
Dim objFile

Const ForWriting=2
Const ForReading=1

Set objFso=createObject("Scripting.FileSystemObject")
Set objFile = objFso.OpentextFile("C:\temp\AD.INI",ForReading)
While not objFile.AtEndOfStream
rline=objFile.Readline()
If (Instr(rline,"[mmtest]")<>0) Then
' we have the line
ReadINI=objFile.Readline()
Exit Function
End If
Wend
End Function


Can anybody help me in this regard?

Thanx in advance,
AD

mayayana

unread,
Apr 10, 2008, 9:16:52 AM4/10/08
to
It's easier to just read it all out and split it into
an array: A1 = Split(s, vbCrLf)
Then you can loop through the array searching
for the key [MMETest], and if you have two values
to get you won't have to read the whole file over again.

You might also find this useful if you're doing a lot
of INI accessing:

www.jsware.net/jsware/scripts.php3#classpk

One of the files in the download is a class that reads an
INI out and stores it in a Dictionary, then presents the
INI as an object, providing functions like GetINIValue,
WriteINIValue, etc.

Pegasus (MVP)

unread,
Apr 10, 2008, 1:31:36 PM4/10/08
to

"Ansu" <ansuma...@gmail.com> wrote in message
news:3cfa53d2-c7f1-4701...@a5g2000prg.googlegroups.com...

If you intend to keep your current code then you could
do this:
If (InStr(LCase(rline),"[mmtest]") > 0) Then
Line=Split(objFile.ReadLine, "=")
if ubound(Line) > 0 then WScript.Echo Line(1)
Exit Function
End If


mr_unreliable

unread,
Apr 10, 2008, 6:13:19 PM4/10/08
to
Ansu wrote:
> Hello everybody,
>
> I am trying to read an MS Window ini file exaple of INI file, ad.ini
>

hi Ansu, back in the day when I was using ini files, I wrote
some general-purpose "class code" to deal with ini files.

There are a few outstanding quibbles, but for better-or-worse
here it is (see attachment).

Uh, you wanted documentation? [Spoken Arrogantly] the code
is "self-documenting"...

fwiw, I prefer xml files now...

cheers, jw
____________________________________________________________

You got questions? WE GOT ANSWERS!!! ..(but,
no guarantee the answers will be applicable to the questions)

clsIniFile.vbs.txt

Ansu

unread,
Apr 11, 2008, 12:56:59 AM4/11/08
to
On Apr 11, 3:13 am, mr_unreliable <kindlyReplyToNewsgr...@notmail.com>
wrote:
> [clsIniFile.vbs.txt]Option Explicit
> '
> ' ================================================
> ' ================================================
> ' A WSH INI (INITIALIZATION) FILE CLASS OBJECT ===
> ' ================================================
> ' ================================================
> '
> ' Acknowledgment: vbParadise provided the "model" (as vb Code)
> ' for this utility. However this is not a one-for-one translation
> ' of the vbParadise code. It was almost entirely re-written,
> ' to conform with the ideosyncracies of vbScript,
> ' (and my own ideosyncracies as well)...
> '
> ' --- Revision History ---------------------------
> ' 20May02: Initial Attempt.
> ' 22May02: added code to deal with comments.
> ' 26May02: revised search pattern, to better detect blank lines,
> ' (courtesy sFulton of wsh ng)...
> ' 03July02: repaired typing mistake...
> ' 06July02: repaired EnumKeys (was returning comment lines)...
> ' 04Feb03: allowed for "commenting out" key=value pair. (thx to: j. ruzewski)
> ' 11Sept03: repaired DeleteKey (vielen Dank, Herr Doktor Reeg)...
> ' --- end of revisions ---------------------------
>
> Class clsIniFile
>
> ' "class level" variables
> Private c_bDebug ' as boolean
> Private c_sMe ' for debugging messages...
> '
> Private c_oRE ' as object (regexp)
> Private c_oDB ' as object (dictionary)
> Private c_sLines ' as collection (ini file lines)
> '
> Private c_sBuffer ' as string (holds entire "resource")
> Private c_iLine ' as integer (holds pointer to NEXT line of text)
> '
> Private ptnIniLine ' as string (pattern for line split)
> Private ptnSecHeader ' as string (pattern for section header)
> Private sEmpty ' as string
> '
> Private i,j ' as integer (for use as loop indexes)
> Private c_sComment ' as string
> Private c_sPreamble ' as string
> ' --- end of declarations and constants ----------
>
> ' ----------------------------------------------
> ' --- ADD / DELETE SECTION ---------------------
> ' ----------------------------------------------
>
> Public Function AddSection(sSectionName) ' returns a boolean value
> Dim saBlank(1,0) ' as string array
>
> ' if there is an existing section by this name, then punt...
> if c_oDB.Exists(sSectionName) then
> AddSection = False : Exit Function
> End If
>
> ' this is a new section name, so add it to dic (er, database)...
> saBlank(0,0) = sEmpty : saBlank(1,0) = sEmpty ' make up dummy key/value pair array
> c_oDB.Add sSectionName, saBlank
>
> AddSection = True ' return success value
> End Function
>
> Public Function DeleteSection(sSectionName) ' returns a boolean value
>
> ' if there is NO section by this name, then punt...
> if NOT c_oDB.Exists(sSectionName) then
> DeleteSection = False : Exit Function
> End If
>
> ' delete this section...
> c_oDB.Remove(sSectionName)
>
> DeleteSection = True ' return success value
> End Function
>
> ' ----------------------------------------------
> ' --- ADD / DELETE KEY (w/corresponding value) -
> ' ----------------------------------------------
>
> Public Function AddKey(sSection, sKey, sValue) ' returns a boolean value
> Const sMe = "[cIni:AddKey], "
> Dim saKeyValuePairs ' as string array (key/value pairs)
> Dim cItems ' as integer
> Dim saNewKeyValuePairs() ' as string array
> Dim sLastKey, sLastValue ' as string(s)
>
> ' if there is NO section by this name, then punt...
> if NOT c_oDB.Exists(sSection) then
> AddKey = False : Exit Function
> End If
>
> saKeyValuePairs = c_oDB(sSection) ' retrieve key/value pairs
> if IsArray(saKeyValuePairs) then ' test valid array of key/value pairs
> cItems = UBound(saKeyValuePairs, 2) + 1 ' add one to expand array
> if c_bDebug then dbPrint sMe & "cItems in section: " & CStr(cItems-1) & "/" & sSection
>
> ' create a new string array...
> ReDim saNewKeyValuePairs(1,cItems)
>
> ' copy the existing key/value pairs...
> For j = 0 to cItems -1
> saNewKeyValuePairs(0,j) = saKeyValuePairs(0,j)
> saNewKeyValuePairs(1,j) = saKeyValuePairs(1,j)
> Next ' j
>
> ' add the new key/value pair at end,
> ' (note: if last key/value is comment, then insert AHEAD of the comment)...
> sLastKey = saKeyValuePairs(0,cItems -1)
> sLastValue = saKeyValuePairs(1,cItems -1)
> if c_bDebug then dbPrint sMe & "sLastKey/sLastValue: " & sLastKey & "/" & sLastValue
>
> ' test for comment line or blank line...
> if sLastKey = c_sComment then ' test for comment line...
>
> saNewKeyValuePairs(0,cItems -1) = sKey
> saNewKeyValuePairs(1,cItems -1) = sValue
> ' dbPrint "[], added: " & CStr(cItems -1) & "/" & sKey & "/" & sValue
>
> saNewKeyValuePairs(0,cItems) = sLastKey ' tack on the blank/comment
> saNewKeyValuePairs(1,cItems) = sLastValue
>
> Else ' last key/value NOT blank/comment...
>
> saNewKeyValuePairs(0,cItems) = sKey ' add new key/value to end...
> saNewKeyValuePairs(1,cItems) = sValue
> End If ' blank/comment test
>
> Else ' not a valid array, so make one...
>
> cItems = 0 ' zero means one, in zero-based arrays
> ReDim saNewKeyValuePairs(1,cItems)
>
> saNewKeyValuePairs(0,cItems) = sKey ' add new key/value to end...
> saNewKeyValuePairs(1,cItems) = sValue
>
> End If ' valid array test
>
> ' replace the key/value pairs in the DB...
> c_oDB.Item(sSection) = saNewKeyValuePairs
>
> AddKey = True
> End Function
>
> Public Function DeleteKey(sSection, sKey) ' returns a boolean value
> Const sMe = "[cIni:DeleteKey], "
> Dim saKeyValuePairs ' as string array (key/value pairs)
> Dim cItems ' as integer
>
> ' if there is NO section by this name, then punt...
> if NOT c_oDB.Exists(sSection) then
> DeleteKey = False : Exit Function
> End If
>
> saKeyValuePairs = c_oDB(sSection) ' retrieve key/value pairs
> if NOT IsArray(saKeyValuePairs) then ' test no key/value pairs
> DeleteKey = False : Exit Function
> End If
>
> cItems = UBound(saKeyValuePairs, 2)
> if c_bDebug then dbPrint sMe & "cItems in section: " & CStr(cItems) & "/" & sSection
>
> ' search for the sKey entry, in this string array...
> For j = 0 to cItems
>
> if saKeyValuePairs(0,j) = sKey then ' found it!
>
> ' set this key/value to empty (rather than REALLY deleting it)...
> saKeyValuePairs(0,j) = sEmpty
> saKeyValuePairs(1,j) = sEmpty
>
> ' update the section contents, as held in the DB...
> c_oDB.Item(sSection) = saKeyValuePairs ' (vielen Dank, Herr Doktor Reeg)
>
> DeleteKey = True ' return success value
> Exit Function
> End If
> Next ' j
>
> ' at this point you searched through all the keys,
> ' without finding the designated sKey value, so TILT!
> DeleteKey = False
> End Function
>
> ' ----------------------------------------------
> ' --- ENUM SECTIONS (return string array) ------
> ' ----------------------------------------------
>
> Public Function EnumSections()
>
> EnumSections = c_oDB.Keys ' Keys method returns a string array...
> End Function
>
> ' ----------------------------------------------
> ' --- ENUM KEYS (return string array) ----------
> ' ----------------------------------------------
>
> Public Function EnumKeys(sSection)
> Const sMe = "[cInI:EnumKeys], "
> Dim saKeyValuePairs ' as string array (key/value pairs)
> Dim saReturn() ' as string array
> Dim cItems, cActual ' as integer
>
> saKeyValuePairs = c_oDB(sSection) ' retrieve key/value pairs
> BugAssert (IsArray(saKeyValuePairs)), sMe & "no key/value pairs for this section: " & sSection
> cItems = UBound(saKeyValuePairs, 2)
> if c_bDebug then dbPrint "[cIni:EnumKeys], cItems in section: " & CStr(cItems) & "/" & sSection
>
> cActual = 0
> ReDim saReturn(cItems) ' includes actual items (plus any comment lines)...
> For j = 0 To cItems
> if saKeyValuePairs(0,j) <> c_sComment then
> saReturn(cActual) = saKeyValuePairs(0,j)
> cActual = cActual + 1
> End If
> Next
> ReDim Preserve saReturn(cActual - 1)
> EnumKeys = saReturn ' set return (string array)
> End Function
>
> ' ----------------------------------------------
> ' --- GET / SET VALUE --------------------------
> ' ----------------------------------------------
>
> Public Function GetValue(sSection, sKey)
> Const sMe = "[cIni:GetValue], "
> Dim saKeyValuePairs ' as string array
> Dim cItems ' as integer
> Dim sThisKey, sThisValue
>
> if c_bDebug then dbPrint sMe & "sSection/sKey is: " & sSection & "/" & sKey
>
> ' get the section's key/value pairs array...
> saKeyValuePairs = c_oDB(sSection) ' retrieve associated item (a string array)
> BugAssert (IsArray(saKeyValuePairs)), sMe & "no key/value pairs for this section: " & sSection & " / " & sKey
>
> cItems = UBound(saKeyValuePairs, 2)
> if c_bDebug then dbPrint sMe & "cItems: " & CStr(cItems)
> BugAssert (cItems >= 0), sMe & "internal error: no key/value pairs for this section: " & sSection & " / " & sKey
>
> ' lookup the value for this key...
> For j = 0 to cItems
> sThisKey = saKeyValuePairs(0,j)
> sThisValue = saKeyValuePairs(1,j)
> ' dbPrint sMe & "sThisKey/sThisValue: " & sThisKey & "/" & sThisValue
>
> if sThisKey = sKey then ' found it!
> GetValue = sThisValue ' set return value
> Exit Function
> End If
> Next ' j
>
> ' use bugassert to throw an error...
> BugAssert False, sMe & "could not find value for: " & sSection & "/" & sKey
> End Function
>
> Public Function SetValue(sSection, sKey, sNewVal) ' returns a boolean value
> Const sMe = "[cIni:SetValue], "
> Dim saKeyValuePairs ' as string array
> Dim cItems ' as integer
> Dim sThisKey, sThisValue
>
> if c_bDebug then dbPrint sMe & "sSection/sKey is: " & sSection & "/" & sKey
>
> ' get the section's key/value pairs array...
> saKeyValuePairs = c_oDB(sSection) ' retrieve associated item (a string array)
> BugAssert (IsArray(saKeyValuePairs)), sMe & "no key/value pairs for this section: " & sSection & " / " & sKey...
>
> read more »

Thanks everybody for replying my query
AD

Reventlov

unread,
Apr 14, 2008, 4:50:12 PM4/14/08
to
Il giorno Wed, 9 Apr 2008 23:27:50 -0700 (PDT), Ansu <ansuma...@gmail.com> ha scritto:

>Hello everybody,
>
>I am trying to read an MS Window ini file exaple of INI file, ad.ini

Hi,
If you use Vbsedit (adersoft) there are a lot of scripts you can use. One of them reads
the ini files.

If you have Word installed, you can use the getprofilestring function.( FileName As
String, Section As String, Key As String)

The FileName argument corresponds to the old INI file name; for the Registry, it is a
zero-length string (''"). The Section (the part starting with HKEY_LOCAL_MACHINE) is the
Registry folder. The Key (WinDir) is the entry in the Name column of the right pane of
the Registry folder.

Set objWord = CreateObject("Word.application")

WinDir = objWord.System.PrivateProfileString(FileName , Section , Key)


--
Giovanni Cenati (Bergamo, Italy)
Write to "Reventlov" at katamail com
http://digilander.libero.it/Cenati (Esempi e programmi in VbScript)
--

0 new messages