Results 1 to 10 of 10

Thread: Alphabetically sort ini file

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Location
    Adelaide, South Australia
    Posts
    1

    Alphabetically sort ini file

    Is it possible to alphabetically or alphanumerically sort the Section Names in an ini file? Currently, if my understanding is correct, if you add a new Section Name it is appended to the end of the ini file. I want to populate an array of Command Buttons using the Section Names as the caption for the buttons and I would like to have them in alphabetical order if possible.

    Thanks
    Ian

  2. #2
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051
    I think you'd have to open the ini file as a normal fine, then parse it, and re-write the file.
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  3. #3

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135
    I don't think you need any array at all - if section name = button.caption then that would be all you need to find the value. "Sorting" wouldn't make any diffrence in peformance.

  5. #5
    Member
    Join Date
    Mar 2004
    Location
    Texas
    Posts
    53
    Originally posted by RhinoBull
    I don't think you need any array at all - if section name = button.caption then that would be all you need to find the value. "Sorting" wouldn't make any diffrence in peformance.
    While he's right, I too like 'organization'.
    Originally posted by SLH

    I think you'd have to open the ini file as a normal fine, then parse it, and re-write the file.
    This is the general idea. For example you could do this:

    Code:
    FILE.INI contains:
    
    [A Section]
    name = v1
    size = 25
    
    [B Section]
    name = be5
    size = 16
    Now make your own data structure that resembles this format, A String for the section name, a integer for the size and a String for the name.

    After doing that you will need to open the .ini and parse the data into an array of the structures you just made. I dont know much about structures in VB, so I cant offer code to do that, but pseudo code for reading in the file would be:
    Code:
    int cur index = -1
    
    While not EOF
    {
    GetLine
    if line is a section heading (seach the string for [ and ] )
        increment index       
        assign that value to the current strucure var (index)
    if(line . Find "name=")
        assign to name of the current strucure var (index)
    else if (line . find "size=")
        convert to int and assign to size of current structure var (index)
    }
    I hope you understand the general idea. Good luck!
    Good programming site:
    *http://www.planet-source-code.com

    Our CS Clan Page:
    *http://h2p.inter-gamer.com/index.html

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135
    Originally posted by Insane_Magician
    ... I hope you understand the general idea...
    So far it's all about over complicating very simple things.

  7. #7
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,669
    There are API commands for getting ALL of the section names in an .ini file, as well as for getting all of a section's data. You could use those to retrieve the section names and the section data, sort the names, and re-write the sorted file data.

  8. #8
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    430
    I could use a little more info on this subject. What API's?

  9. #9
    Frenzied Member jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    1,669
    In a module:

    VB Code:
    1. Declare Function GetPrivateProfileSectionNames Lib "Kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal strTypeList As String, ByVal dblListLen As Long, ByVal strIniFileName As String) As Long
    2. Declare Function GetPrivateProfileSection Lib "Kernel32" Alias "GetPrivateProfileSectionA" (ByVal strSelSectName As String, ByVal strSectData As String, ByVal dblListLen As Long, ByVal strIniFileName As String) As Long
    3. Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" (ByVal strSelSectName As String, ByVal strKeyName As String, ByVal strKeyDefault As String, ByVal strKeyValue As String, ByVal dblListLen As Long, ByVal strIniFileName As String) As Long
    4. Declare Function GetWindowsDirectory Lib "Kernel32" Alias "GetWindowsDirectoryA" (ByVal strWinDir As String, ByVal dblListLen As Long) As Long
    5. Declare Function WritePrivateProfileSection Lib "Kernel32" Alias "WritePrivateProfileSectionA" (ByVal strSelSectName As String, ByVal strSectData As String, ByVal strIniFileName As String) As Long
    6. Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" (ByVal strSelSectName As String, ByVal strKeyName As String, ByVal strKeyValue As String, ByVal strIniFileName As String) As Long

    In a form:
    VB Code:
    1. ' Get a section
    2. strSectionName = "Printers"
    3. strSectionData = String$(4096, Chr$(0))
    4. lngListLen2 = GetPrivateProfileSection(strSectionName, strSectionData, Len(strSectionData), strIniFilePath + strIniFileName)
    5. ' Get section names
    6. strPrintJobLIst = String$(4096,Chr$(0))
    7. lngListLen = GetPrivateProfileSectionNames(strPrintJobList, Len(strPrintJobList), strIniFilePath + strIniFileName)

  10. #10
    Hyperactive Member
    Join Date
    Jan 2001
    Posts
    430
    Thanks, I'll play with this.....

    It's a good start.

Posting Permissions

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



Click Here to Expand Forum to Full Width