Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 688
  • Last Modified:

Reading an INI file using VBScript

I'm looking for code to read an INI file in VBScript.

Is there something already in VBScript that'll let me do this, or will I need to use FSO/TextStream/etc. for it?

I would prefer to read it into something like a Dictionary/Collection type object but not essential.

Please provide sample code to illustrate.
1 Solution
U can use FSO in ur project to read and write into files and for all other file manipulation activities.
Please refer following sample code to explain FSO.

'Set References to Microsoft Scripting Runtime in your VB project.

strFileName = path to ini

Private Function AutoLogin(ByVal strFileName As String) As Boolean
Dim Fso As New FileSystemObject
Dim TxtStream As TextStream
Dim strReadLine As String
Dim strReadAll As String
Dim blnGotLogin As Boolean
Dim intPos As Long
Dim blnReplaceFile As Boolean

Set TxtStream = Fso.OpenTextFile(strFileName, ForReading)
Do Until TxtStream.AtEndOfStream = True
    strReadLine = TxtStream.ReadLine
    If blnGotLogin = False Then
        'you must obviously know what you are looking for
        'if you wish to adjust to check for more than just "AutoLogin" then just follow
        'same procedure
        intPos = InStr(1, strReadLine, "Auto Login=")
        If intPos <> 0 Then
            blnGotLogin = True
            'check if the option has been set
            If Len(strReadLine) < intPos + 1 Then
                strReadLine = "Auto Login=1"
                blnReplaceFile = True
                'check if a 1 has been set
                If InStr(intPos + 1, strReadLine, "1") <> 0 Then
                    AutoLogin = True
                    'no need to read anymore unless something else is required from ini
                    Exit Do
                    'if you want to set auto login
                    strReadLine = "Auto Login=1"
                    blnReplaceFile = True
                End If
            End If
        End If
    End If
    'add end of line character
    strReadLine = strReadLine & vbCrLf
    strReadAll = strReadAll & strReadLine


If blnReplaceFile = True Then
    Fso.DeleteFile strFileName, True
    Set TxtStream = Fso.OpenTextFile(strFileName, ForWriting, True)
    TxtStream.Write strReadAll
End If

End Function

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now