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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

built-in function for editing ini files?

Status
Not open for further replies.

guru533

MIS
Aug 22, 2002
25
US
Does anybody know if vbscript has a built-in function for editing ini files?
 
the below is the best i have.
pass it the ini file name and the section you are interested in and you get a dict object.

i know its not editing but i use it alot so might be of some use.



Function ParseINI(strIniFileName, strSection2Find)

Set FSO = WScript.CreateObject("Scripting.FileSystemObject")
'strIniFileName = "d:\vbs\xp\w2kinst.ini"

Err.Clear
Set tsIni = FSO.OpenTextFile(strIniFileName)

If Err.Number <> 0 Then
msgbox &quot;cant find ini file&quot;
End If

Do While Not tsIni.AtEndOfStream
sLine = &quot;&quot;
sLine = Trim(tsIni.ReadLine)
If sLine <> &quot;&quot; Then
If Left(sLine,1) <> &quot;;&quot; Then


If Left(sLine,1) = &quot;[&quot; Then

'Msgbox sLine & &quot; section found&quot;
strSection = Left(sLine, Len(sLine) - 1)
strSection = Right(strSection, Len(strSection) - 1)
If LCase(strSection) = LCase(strSection2Find) Then
Set ParseINI = Wscript.CreateObject(&quot;Scripting.Dictionary&quot;)
End If

Else
'key and value logic
intEquals = InStr(1, sLine, &quot;=&quot;)
If (intEquals <= 1) Then
'msgbox &quot;error: the following line is invalid &quot; & sLine
Else
'weve found a valid line
sKey = Left(sLine, intEquals - 1)
sVal = Right(sLine, Len(sLine) - intEquals)

If LCase(strSection) = LCase(strSection2Find) Then
Err.Clear
ParseINI.Add sKey, sVal
If Err.Number <> 0 Then
msgbox &quot;unable to add to dictionary object&quot;
End If
'msgbox strSection & &quot; &quot; & sKey & &quot;;;;;&quot; & sVal
Else
'msgbox strSection & &quot; &quot; & skey & &quot;;;;;&quot; & sVal
End If

'key and value logic end if
End If
End If
End If
End If
Loop

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top