Jump to content

is there a sort ini function?


Recommended Posts

Hi guys,

Before I go about making one for myself, is anyone aware of a function or script available for sorting the sections of an ini file?

Thanks,

Spook

P.S. I did attempt to do a search, but "ini" is too short of a search criteria and "sort" gets way too many hits on its own.

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

How about this:

#include <Array.au3>

$sections = IniReadSectionNames("ini.ini") ;this will return an array containing one element for each section, now just sort it!
$sorted = _ArraySort($sections)

:):guitar::)

Edited by sandman

[center]"Yes, [our app] runs on Windows as well as Linux, but if you had a Picasso painting, would you put it in the bathroom?" -BitchX.com (IRC client)"I would change the world, but they won't give me the source code." -Unknownsite . blog . portfolio . claimidcode.is.poetry();[/center]

Link to comment
Share on other sites

Thanks for the reply,

Yeah thats the basis of how I would build it... with the addition of then gathering all the data under a given section (sorting that as well) and writing it out to a new ini file.

However if its already been done by someone, I can save myself some work. Figured it was worth asking about anyway.

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

  • Moderators

#include <array.au3>
$file = @DesktopDir & '\test.ini'
_IniSort($file)

Func _IniSort($hIni, $iAscend = 1)
    Local $aIRSN = IniReadSectionNames($hIni)
    If Not IsArray($aIRSN) Then Return SetError(1, 0, 0)
    _ArraySort($aIRSN, 0, $iAscend)
    Local $sHold
    For $iCC = 1 To UBound($aIRSN) - 1
        Local $aIRS = IniReadSection($hIni, $aIRSN[$iCC])
        If Not IsArray($aIRS) Then ContinueLoop
        _ArraySort($aIRS, 0, $iAscend, 0, 2)
        $sHold &= '[' & $aIRSN[$iCC] & ']' & @CRLF
        For $xCC = 1 To $aIRS[0][0]
            $sHold &= $aIRS[$xCC][0] & '=' & $aIRS[$xCC][1] & @CRLF
        Next
    Next
    If $sHold Then
        $sHold = StringTrimRight($sHold, 2)
        FileClose(FileOpen($hIni, 2))
        FileWrite($hIni, $sHold)
        Return 1
    EndIf
    Return SetError(1, 0, 0)
EndFunc

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

SmOke_N, I don't care what Valik says about you, your OK in my book :)

Thanks man

Edited by SpookMeister

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

@SmOke_N

If the Ini file has an empty section in it, eg:

[one]
this = that
that = what

[two]

[three]
more=less
less = lesser still

Your function entirely removes the empty section, in this case [two]

Was justing testing it against a early one that I had written, but mine made use of a temporary file for holding the results. Also, mine preserved single empty lines between sections for easier human readability. I think I'll see if I can't adapt yours to give results like mine :)

Thanks for this and all your great contributions!

Edited by ResNullius
Link to comment
Share on other sites

OK,

Here's my modification of SmOke_N's function.

This preserves section names with no entries, and adds a blank line between before each section for easier human readability:

#include <array.au3>

Func _IniSort($hIni, $iAscend = 1)
    Local $aIRSN = IniReadSectionNames($hIni)
    If Not IsArray($aIRSN) Then Return SetError(1, 0, 0)

    _ArraySort($aIRSN, 0, $iAscend)
    Local $sHold

    For $iCC = 1 To UBound($aIRSN) - 1
       Local $aIRS = IniReadSection($hIni, $aIRSN[$iCC])
       $sHold &= '[' & $aIRSN[$iCC] & ']' & @CRLF 
       If Not IsArray($aIRS) Then 
             $sHold &= @CRLF
             ContinueLoop
       EndIf
       _ArraySort($aIRS, 0, $iAscend, 0, 2)
       For $xCC = 1 To $aIRS[0][0]
           $sHold &= $aIRS[$xCC][0] & '=' & $aIRS[$xCC][1] & @CRLF
       Next
       $sHold &= @CRLF
    Next

    If $sHold Then
       $sHold = StringTrimRight($sHold, 2)
       FileClose(FileOpen($hIni, 2))
       FileWrite($hIni, $sHold)
       Return 1
    EndIf

    Return SetError(1, 0, 0)
EndFunc

Hope you don't mind SmOke_N.

Edited by ResNullius
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...