+ Reply to Thread
Results 1 to 7 of 7

Read and write INI files

Hybrid View

WHWALDREP Read and write INI files 09-21-2009, 10:02 AM
T-J Re: Read and write INI files 09-22-2009, 04:20 AM
WHWALDREP Re: Read and write INI files 09-22-2009, 11:39 AM
Leith Ross Re: Read and write INI files 09-22-2009, 01:02 PM
WHWALDREP Re: Read and write INI files 09-26-2009, 02:45 AM
  1. #1
    Registered User
    Join Date
    12-14-2007
    Location
    Georgia
    Posts
    78

    Read and write INI files

    Ok I have these ini files for a access control system. We have 40 different systems. All on different databases. that look like the following:
    [1243052037]
    group = "master"
    names = "Wesley,John,Bill"
    protected = 1
    
    [776843634]
    group = "member"
    names = "Test,Adam,Joe"
    protected = 0
    
    [751211588]
    group = "user"
    names = "Karen,Dell,HP"
    protected = 0
    So I want to read these in to an excel sheet like I have attached. Sort them and purge them togeather. and then write the ini file back out to update the system. Thank you for your help.
    Attached Files Attached Files
    Excel 2007 SP1

  2. #2
    Valued Forum Contributor
    Join Date
    08-26-2006
    Location
    -
    MS-Off Ver
    2010
    Posts
    388

    Re: Read and write INI files

    Hi

    For reading the .ini files these Windows API functions could be useful:

    GetPrivateProfileSection
    GetPrivateProfileSectionNames
    GetPrivateProfileString

    and for writing:

    WritePrivateProfileSection
    WritePrivateProfileString

    There are plenty of examples on the www.

  3. #3
    Registered User
    Join Date
    12-14-2007
    Location
    Georgia
    Posts
    78

    Re: Read and write INI files

    yes I have seen some examples, but not sure how to loop thru them. Everthing I have seen pertains to getting a value that is known.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Read and write INI files

    Hello WHWALDREP,

    If you post a copy of your workbook in Excel 2003 format, I can help you with this.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Registered User
    Join Date
    12-14-2007
    Location
    Georgia
    Posts
    78

    Re: Read and write INI files

    Thank you.
    Attached Files Attached Files

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Read and write INI files

    Hello WHWALDREP,

    I have written 2 macro modules. One contains the API calls for the Provate Profile functions and the second contains the "ReadINI" and "WriteINI" commands that are attached to the buttons. The code for this macros is below. For those who are interested about the Private Profile functions, download the the workbook and read the notes I have included in the module "Read_Write_INI_Files".

    You should know that init files typically don't use quotes as there are only 2 data types: Strings and Integers. The test file I created based on your example did not include the quotes. The empty line between sections was also removed as this doesn't conform to the API standard. It will read the information correctly, but the API WriteProfile functions don't include blanks between sections. Here is what the file looks like:

    [bInitialization File Layout[/b]
    [1243052037]
    group = master
    names = Wesley,John,Bill
    protected = 1
    [776843634]
    group = member
    names = Test,Adam,Joe
    protected = 0
    [751211588]
    group = user
    names = Karen,Dell,HP
    protected = 0
    Macro Code to Read aand Write the Init File
    Public FileName As String
    
    Sub ReadINI()
    
      Dim Buffer1 As String
      Dim Buffer2 As String
      Dim BuffSize As Long
      Dim ColOrder As Variant
      Dim FileFilters As String
      Dim I As Integer
      Dim Key As String
      Dim Keys As Variant
      Dim L1 As Long
      Dim ListObj As ListObject
      Dim RetVal As Variant
      Dim Rng As Range
      Dim SectionName As String
      
        If FileName = "" Then
          'Select and Open the file.
           FileFilters = "Initialization Files (*.ini),*.ini,Text Files (*.txt), *.txt, All Files (*.*), *.*"
           RetVal = Application.GetOpenFilename(FileFilters, 1, "Select File")
             If RetVal = "False" Then
                Exit Sub
             Else
                FileName = RetVal
             End If
        End If
        
          Keys = Array("group", "protected", "names")
          ColOrder = Array(2, 3, 4)
          
          Set ListObj = ActiveSheet.ListObjects("Table1")
          Set Rng = ListObj.Range
           'Ignore the header and total rows.
            Set Rng = Rng.Columns(1).Offset(1, 0).Resize(RowSize:=Rng.Rows.Count - 2)
            R = 1
            C = 1
            
           'Create two 8k buffers to hold the Section Names and Keys/Values.
            BuffSize = 8192
            Buffer1 = Space(BuffSize)
            Buffer2 = Space(BuffSize)
            
           'Read in the Section Names.
            RetVal = GetPrivateProfileSectionNames(Buffer1, BuffSize, FileName)
             'Copy the Section Names to the ListObject and save in an array.
              Do
                L1 = StrLen(Buffer1)
                If L1 = 0 Then Exit Do
                SectionName = Left(Buffer1, L1)
                Rng.Cells(R, C) = SectionName
                  For I = 0 To UBound(Keys)
                    Key = Keys(I)
                    RetVal = GetPrivateProfileString(SectionName, ByVal Key, "?", Buffer2, BuffSize, FileName)
                    C = C + 1
                    Rng.Cells(R, C) = Left(Buffer2, RetVal)
                    Buffer2 = Space(BuffSize)
                  Next I
                C = 1
                R = R + 1
                Buffer1 = Right(Buffer1, Len(Buffer1) - L1 - 1)
              Loop
          
    End Sub
    
    Sub WriteINI()
    
      Dim ColOrder As Variant
      Dim FileFilters As String
      Dim Key As String
      Dim Keys As Variant
      Dim ListObj As ListObject
      Dim ProfileString As String
      Dim RetVal As Variant
      Dim Rng As Range
       
        If FileName = "" Then
           MsgBox "There is No File Selected to Write to." & vbCrLf _
                 & "Please Select a File."
          ' Select and Open the file.
           FileFilters = "Initialization Files (*.ini),*.ini,Text Files (*.txt), *.txt, All Files (*.*), *.*"
           RetVal = Application.GetOpenFilename(FileFilters, 1, "Select File")
             If RetVal = "False" Then
                Exit Sub
             Else
                FileName = RetVal
             End If
        End If
          
          Keys = Array("group", "names", "protected")
          ColOrder = Array(2, 4, 3)
        
          Set ListObj = ActiveSheet.ListObjects("Table1")
          Set Rng = ListObj.Range
          
           'Ignore the header and total rows.
            Set Rng = Rng.Offset(1, 0).Resize(RowSize:=Rng.Rows.Count - 2)
            
           'Overwrite the file by deleting it and recreating it.
            Kill FileName
            N = FreeFile
            Open FileName For Output As #N
            Close #N
            
              For R = 1 To Rng.Rows.Count
                SectionName = Trim(Rng.Cells(R, 1))
                If SectionName <> "" Then
                   ProfileString = ""
                     For C = 0 To UBound(ColOrder)
                       Key = Keys(C) & " = "
                       ProfileString = ProfileString & Key & Rng.Cells(R, ColOrder(C)) & Chr$(0)
                     Next C
                   ProfileString = ProfileString & Chr$(0)
                   RetVal = WritePrivateProfileSection(SectionName, ProfileString, FileName)
                End If
              Next R
            
    End Sub
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1