Tip

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
spaces
INI_Variable = Ucase$(Trim$(INI_Variable)) ' convert to uppercase and trim
spaces

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
Loop
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
variable
If Left$(IniFileLines(ArrayIndex2),1) = "[" And
Right$(IniFileLines(ArrayIndex2),1) = "]" Then ' found next section without
finding variable
Exit For
Else
' 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 =
Instr(IniFileLines(ArrayIndex2),"=")
If Not EqualSignPosition = 0 Then
If
Trim(Left$(IniFileLines(ArrayIndex2),EqualSignPosition - 1)) = INI_Variable
Then ' found correct variable
VariableFound = True
Print "INI Read Successf

This was first published in November 2000

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
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
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.