Update Ini File.

This is a procedure to update Inifiles. It will create the inifile if it does not exist.


'******************************************************************************
'** Script:         WriteIni.vbs
'** Version:      1.0
'** Created:      29/11/2008 10:55AM
'** Author:         Adriaan Westra
'** E-mail:         
'**
'** Purpose / Comments:
'**               Demonstrate writing to an inifile.
'**
'** Doel / Commentaar:
'**               Demoscript voor het gebruik van de procedure Writeini.
'**
'**
'** Changelog :
'** date / time            : 
'** 29/11/2008 10:55AM  :   Initial version
'**
'******************************************************************************

'******************************************************************************
'**   Constants for opening files
'**   Constanten voor het openen van files
Const ForWriting = 2 
Const ForAppending = 8

'******************************************************************************
'**   Call sub WriteIni to update inifile
'**   Roep de subroutine WriteIni aan om de inifile aante passen
WriteIni "test.ini","Main", "DateLastAccessed", Now

'******************************************************************************
'**   Exit script
'**   Verlaat script
Wscript.Quit()

'******************************************************************************
'** Function:      WriteIni
'** Version:      1.0
'** Created:      29/11/2008 10:55AM
'** Author:         Adriaan Westra
'** E-mail:         
'**
'** Purpose / Comments:
'**
'**      Update inifile. 
'**      If the Section not exists the Section and value will be written
'**      at the end of the file. 
'**      If the section exists but the value does not exists, the value 
'**      will be inserted at the end of the section.
'**      If the value exists it will be updated.
'**      If the file does not exist, it will be created.
'**
'**
'** Change Log :
'**
'** 29/11/2008 10:55AM : Initial Version
'** 01/12/2008 14:30   : Added loop to skip blank lines.
'**
'** Arguments :  
'**
'**   strIniFile   :   Name of the file to update
'**   strSection   :   Name of the Section to update
'**   strKey      :   Name of the value to update
'**   strValue      :   The value to update
'**
'** Returns   :
'**
'**   Nothing
'**         
'******************************************************************************
Sub WriteIni(strIniFile, strSection, strKey, strValue)

 Dim objFSO, objIniFile          ' Define Filesystemobject and Textfile object
 Dim strFileText                   ' Define Filetext
 Dim arrLines, arrTemp            ' Define arrays
 Dim intI, intJ                  ' Define counters
 Dim bValueFound, bSectionFound   ' Define booleans 

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 If objFSO.FileExists(strIniFile) Then
 
    Set objIniFile = objFSO.OpenTextFile(strINIFile)
 

   If Not objIniFile.AtEndOfStream Then
      strFileText = objIniFile.ReadAll

      arrLines = Split(strFileText, vbCrLf)


      For intI = 0 to Ubound(arrLines)
         If InStr(arrLines(intI), "[" ) Then
            
            If UCase(arrLines(intI)) = "[" & UCase(strSection) & "]" Then
               bSectionFound = True
              Else
                 If bSectionFound Then
                    If Not bValueFound = True Then
                       arrTemp = arrLines
                       For intJ = intI - 1 to 0 step -1
                           If Not Trim(arrLines(intJ)) = "" Then
                              intI = intJ + 1
                              Exit For 
                           End if
                       Next
                       ReDim arrLines(UBound(arrLines) + 1)
                       For intJ = 0 to UBound(arrTemp)
                          If intJ = intI Then
                             arrLines(intJ) = strKey & "=" & strValue
                             arrLines(intJ + 1) = arrTemp(intJ)
                          Else
                             If intJ > intI Then
                                arrLines(intJ + 1) = arrTemp(intJ)
                             Else
                                arrLines(intJ) = arrTemp(intJ)
                             End If
                          End If
                       Next
                       bValueFound = True
                    End If
                  Exit For
               End If
            End If
         Else
            If bSectionFound = True Then
               If Left(arrLines(intI), Len(strKey)+1)=strKey & "="  Then
                   arrLines(intI) = strKey & "=" & strValue 
                   bValueFound = True
                   Exit For
                End If
             End If
          End If
      Next

    End If
   
    objIniFile.Close
 Else
    ReDim arrLines(2)
    arrLines(0) = "[" & strSection & "]"
    arrLines(1) = strKey & "=" & strValue
    bValueFound = True
 End If 
 If Not bValueFound Then
    arrTemp = arrLines
    intI = Ubound(arrLines)
    For intJ = intI to 0 step -1
        If Not Trim(arrLines(intJ)) = "" Then
           intI = intJ + 1
           Exit For 
        End if
    Next 
    If Not bSectionFound = True Then
       intI = intI + 2	
       ReDim Preserve arrLines(UBound(arrLines) + 2)    
    Else
       intI = intI + 1
       ReDim Preserve arrLines(UBound(arrLines) + 1)
    End If
    For intJ = UBound(arrLines) to intI step -1
    	If Not bSectionFound = True Then
           arrLines(intJ) = arrLines(intJ - 2)
        Else
           arrLines(intJ) = arrLines(intJ - 1) 
        End If
    Next
    If Not bSectionFound = True Then
       arrLines(intI - 2) = "[" & strSection & "]" 
       arrLines(intI - 1) = strKey & "=" & strValue 
    Else
       arrLines(intI - 1) = strKey & "=" & strValue 
    End If
 End If 
 
 strFileText = Join(arrLines, vbCrLf)
 Set objIniFile = objFSO.OpenTextFile(strINIFile, ForWriting, True)
 objIniFile.Write strFileText
 
 objIniFile.Close
End Sub