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

Read from INI File 2

Status
Not open for further replies.

TheLad

Technical User
Aug 3, 2001
3,846
GB
I am trying to write some code that will get the required information from an INI file created by me. The INI file will look something like this:

Code:
[AD]
USERSCONTAINER=USERS
DEPARTMENTCONTAINER=BILLING

[USER1]
USERID=BILL01
NAME=Billing ID 01

[USER2]
USERID=BILL02
NAME=Billing ID 02
I have searched the forum and found thread329-1249823 written by stevio and it appears to describe what I need to do. There are amendments to the code posted by tsuji which I have also included but I am still having difficulty with the code. Firstly, without the amendments, the script will work but will hang (or continue searching) if the required [SECTION] cannot be found in the INI file. With the amendments, the script will also hang if the required [SECTION] cannot be found but also it does not pick up the required variables.

Please can anyone help with this?

--------------------------------------
"Insert funny comment in here!"
--------------------------------------
 
I had seen other posts on my searches of the Internet which indicated that as a possible alternative, but I do not know much about XML so if you have some tips you could offer on XML I would be most appreciated.

--------------------------------------
"Insert funny comment in here!"
--------------------------------------
 
i have since moved to xml but this what i would use

Set dicGlobal = ParseAllINI(WshShell.ExpandEnvironmentStrings("%logonserver%\netlogon\global.ini"))

For Each aSection In dicGlobal
Wscript.Echo aSection & "=sectionname"
For Each aKey In dicGlobal.Item(aSection)
Wscript.Echo vbTab & aKey & "=" & dicGlobal.Item(aSection).Item(aKey)
Next
Next


Function ParseAllINI(strIniFileName)
On Error Resume Next
Dim ParseAINI, blnFoundSection, strSection
Dim intEquals, sKey, sVal, i, sLine, tsIni

blnFoundSection = False
Err.Clear

If FSO.FileExists(strIniFileName) Then

Set tsIni = FSO.OpenTextFile(strIniFileName)
Set ParseAllINI = WScript.CreateObject("Scripting.Dictionary")

Do While Not tsIni.AtEndOfStream
sLine = ""
sLine = Trim(tsIni.ReadLine)
If sLine <> "" Then
If Left(sLine,1) <> ";" Then
If Left(sLine,1) = "[" Then
blnFoundSection = True
'Msgbox sLine & " section found"
strSection = Left(sLine, Len(sLine) - 1)
strSection = Right(strSection, Len(strSection) - 1)
Set ParseAINI = Wscript.CreateObject("Scripting.Dictionary")
ParseAllINI.Add UCase(strSection), ParseAINI
Else
'key and value logic
intEquals = InStr(1, sLine, "=")
If (intEquals <= 1) Then
'msgbox "error: the following line is invalid " & sLine
Else
'weve found a valid line
sKey = Left(sLine, intEquals - 1)
sVal = Right(sLine, Len(sLine) - intEquals)


Err.Clear
ParseAINI.Add Trim(LCase(sKey)), Trim(sVal)
If Err.Number <> 0 Then
'msgbox "unable to add to dictionary object"
End If
'msgbox strSection & " " & sKey & ";;;;" & sVal


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

tsIni.Close
Set tsIni = Nothing

If blnFoundSection = False Then
Set ParseAllINI = Wscript.CreateObject("Scripting.Dictionary")
End If

Else
Set ParseAllINI = Wscript.CreateObject("Scripting.Dictionary")
End If


End Function
 
if you wanted to get

[USER2]
NAME=Billing ID 02

then you might use


If dicGlobal.Exists("USER2") Then
If dicGlobal.Item("USER2").Exists("name") Then
Wscript.Echo dicGlobal.Item("USER2").Item("name")
End If
End If
 
Thanks for taking the time to post guys, you've given me something to thing about :)

--------------------------------------
"Insert funny comment in here!"
--------------------------------------
 
Like whether I can THING or THINK !!!!

Doh, long day....

--------------------------------------
"Insert funny comment in here!"
--------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top