Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

INI Manipulation - Read-Check/Edit Value-Save

Status
Not open for further replies.

drweb

IS-IT--Management
Apr 17, 2002
26
0
0
US
Hey All,

I can normally search my way out of scripting roadblocks thanks to these forums and the rest of the net, but am running into getting this accomplished.

Ultimately I need to change some values in a specific section of an INI.

I have tried to use the Classpack from JSware - but can't get things to work correctly.

Here is a sample of the ini file:
[Servers]
Item1=blah
Item2=blah2
Item3=blah3
[LowSpeedConnection]
Item1=0
Item2=0
Item3=0
Item4=1
Item5=0
Item6=0
Item7=0
Item8=0
[ConnSettings]
Item1=10.213.0.67

Using classes and functions I have found, I have no issue getting the INI file into a dictionary object and echoing the [LowSpeedConnection] sections values.
What I can't seem to accomplish is editing those values while in the object and saving back the entire INI file with the new changes.

All I want to do is change the values for each Item(n) key in the [LowSpeedConnection] section to a 1 (the gotcha for me is that there can be a diff. number of Item(n) keys for each file I want to change, not just 1-8 like in my sample)


I may be over complicating things and can't see the simple solution :(


Any help or direction would be greatly appreciated.


Clay
 
Personally, I would store the INI in a series of nested dictionaries. Example to follow shortly.

-Geates



"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Code:
function getValue (dic, strSection, strKey)
	set dicValues = dic.Item(strSection)
	getValue = dicValues.Item(strKey)
end function

sub setValue (dic, strSection, strKey, strValue)
	set dicValues = dic.Item(strSection)
	dicValues.Item(strKey) = strValue
end sub

function loadINI (strFile)
	set objStream = objFSO.OpenTextFile(strFile, 1, true, 0)
	set dicINI = CreateObject("Scripting.Dictionary")
	do while not objStream.AtEndOfStream
		strLine = trim(objStream.ReadLine)
		if ((left(strLine, 1) = "[") and (right(strLine, 1) = "]")) then
			set dicValues = CreateObject("Scripting.Dictionary")
			strHeader = mid(strLine, 2, len(strLine) - 2)
			dicINI.add strHeader, dicValues
		else
			arrTokens = split(strLine, "=")
			dicValues.Add arrTokens(0), arrTokens(1)
		end if 
		if (dicValues.count) then set dicINI.item(strHeader) = dicValues
	loop
	set loadINI = dicINI
end function

function showINI (dic)
	arrKeys = dic.Keys
	for i = 0 to ubound(arrKeys)
		strKey = arrKeys(i)
		if (isObject(dic.Item(strKey))) then 
			strContents = strContents & "[" & strKey & "]" & vbNewLine & showINI(dic.Item(strKey))
		else
			strContents = strContents & strKey & " = " & dic.Item(strKey) & vbNewLine
		end if
	next
	showINI = strContents
end function


set dicINI = loadINI("C:\temp\config.ini")

msgbox showINI (dicINI)
msgbox getValue (dicINI, "LowSpeedConnection", "Item4")
setValue dicINI, "LowSpeedConnection", "Item4", 20
sgbox getValue (dicINI, "LowSpeedConnection", "Item4")

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top