Reading an INI file using VBScript

Posted on 2006-03-28
Last Modified: 2008-03-10
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.
Question by:khooc
    1 Comment
    LVL 9

    Accepted 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

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now