Functions To Set/Get Values From Any Ini File

Notes has the set/get environment variable functions but they only work against
the notes.ini. I wanted functions to set/get values from any ini file. I
wrote three functions to do this and they are included.
Function INI_Read(INI_Path As String, INI_FileName As String,INI_Section As
String,INI_Variable As String,ReturnValue As String) As Variant
' this function will locate a given ini file and locate a variable within
a section. It will return one of the given error
' messages below or the value assigned to the variable.

' to use this function parameters MUST be specified in the given format....
' INI_PATH example ..... "C:\Windows\"
' INI_FileName example .... "notes.ini"
' INI_Section example .... "[Notes]"
' INI_Variable example .... "Directory"
' ReturnValue is used to hold the value of the variable read or the error
message. If the function fails you can find the error message here.
' If it succeeds it will hold the value of the variable to be read.

' return values for errors
' INIRead_Path_Not_Found
' INIRead_File_Not_Found
' INIRead_Section_Not_Found
' INIRead_Variable_Not_Found

INI_Read = True
ReturnValue = ""

INI_Section = Ucase$(Trim$(INI_Section)) ' convert to uppercase and trim
INI_Variable = Ucase$(Trim$(INI_Variable)) ' convert to uppercase and trim

Dim FileName As String
Dim IniFileLines() As String
Dim FileHandle As Integer
Dim TempLine As String
Dim Counter As Long
Dim ArrayIndex1 As Long
Dim ArrayIndex2 As Long
Dim SectionFound As Variant
Dim VariableFound As Variant
Dim EqualSignPosition As Integer

' first thing to do is check for the existence of the path.
On Error 76 Resume Next ' Error is Path not found
FileName = Dir$(INI_Path & "*.*", 0)
If Error() = "Path not found" Then
Print "Path Not Found."
INI_Read = False
ReturnValue = "INIRead_Path_Not_Found"
Exit Function
End If

' second thing to do is check for the existence of the file.
FileName = Dir$(INI_Path & INI_FileName,0)
If FileName = "" Then
Print "File Not Found"
INI_Read = False
ReturnValue = "INIRead_File_Not_Found"
Exit Function
End If

' get a file handle
FileHandle = Freefile
' open the file
Open INI_Path & INI_FileName For Input Access Read As FileHandle

' now read in the file line by line into an array
Counter = 0
Do While Not Eof(FileHandle)
Line Input #FileHandle , TempLine
Redim Preserve IniFileLines(Counter) As String
IniFileLines(Counter) = Ucase$(Trim$(TempLine))
Counter = Counter + 1
Close #FileHandle

' now look for the section
SectionFound = False
VariableFound = False
For ArrayIndex1 = 0 To Counter - 1
If IniFileLines(ArrayIndex1) = INI_Section Then ' found the correct
section... go through array until variable is found or next section is found
SectionFound = True
For ArrayIndex2 = ArrayIndex1 + 1 To Counter - 1 ' look for
If Left$(IniFileLines(ArrayIndex2),1) = "[" And
Right$(IniFileLines(ArrayIndex2),1) = "]" Then ' found next section without
finding variable
Exit For
' check to see if the current line is a comment by
looking for a ";" at the leftmost char
If Not Left$(IniFileLines(ArrayIndex2),1) = ";" Then '
is not a comment
' Locate equal sign in the string
EqualSignPosition =
If Not EqualSignPosition = 0 Then
Trim(Left$(IniFileLines(ArrayIndex2),EqualSignPosition - 1)) = INI_Variable
Then ' found correct variable
VariableFound = True
Print "INI Read Successf
This was last published in November 2000

Dig Deeper on Domino Resources - Part 6

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.




  • iSeries tutorials's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...