Jump to content

Re-order Ini File Entries Alphabetically


jgus
 Share

Recommended Posts

I would like to take an ini file with the following structure......

[1]

exe=regmon.exe

alias=regmon

[2]

exe=filemon.exe

alias=filemon

[3]

exe=accessenum.exe

alia=accessenum

....and write it to another ini file sorted alphabetically by the value in the "alias" key. So it would be sorted like this.

[1]

exe=accessenum.exe

alia=accessenum

[2]

exe=filemon.exe

alias=filemon

[3]

exe=regmon.exe

alias=regmon

My thought was to read the ini data into an array, then sort it, and then iniwrite it back to another ini file. I'm no programmer and there must be something I'm missing with these arrays because I can't seem to accomplish this. Any help would be greatly appreciated as always.

Link to comment
Share on other sites

  • Moderators

My thought was to read the ini data into an array, then sort it, and then iniwrite it back to another ini file. I'm no programmer and there must be something I'm missing with these arrays because I can't seem to accomplish this. Any help would be greatly appreciated as always.

That's pretty much what you would have to do.. Fortunately for you, there is InReadSection(), IniReadSectionNames(), and _ArraySort().

IniReadSection/Names() returns an array already... so you don't have to build one on your own, and you could use _ArraySort() to put them in order. So the tools are there, you just have to play with them :think:.

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

I made an ini file sorter a few weeks back..

#include <GuiConstants.au3>
#include <Array.au3>

GuiCreate("INI Tidy", 392, 166,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS), $WS_EX_ACCEPTFILES )
$Input_1 = GuiCtrlCreateInput("", 20, 30, 220, 20)
GUICtrlSetState ( -1, $GUI_DROPACCEPTED )
$Browse = GuiCtrlCreateButton("Browse", 270, 30, 90, 20)
$Sortit = GuiCtrlCreateButton("Sort my god damn messy ini file out", 100, 60, 260, 30)
$Label = GuiCtrlCreateLabel("", 20, 110, 340, 30)
$tick = GUICtrlCreateCheckbox ("", 65, 70, 20,20)
GuiCtrlSetState (-1 ,$GUI_CHECKED)
GuiCtrlCreateLabel("Sort Section", 20, 58, 80, 15)
GuiCtrlCreateLabel("Names", 20, 72, 40, 15)

GuiSetState()
While 1
    $msg = GuiGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
        ExitLoop
    Case $msg = $Browse
        $inifile = FileOpenDialog ("Select the ini file", @scriptdir, "(*.ini)")
        GUICtrlSetData ($Input_1, $iniFile)
    Case $msg = $Sortit
        If GuiCtrlRead ($Input_1) = ""  Then
            MsgBox (0,"Error","No ini file selected")
        Elseif  FileExists(GuiCtrlRead ($Input_1)) = 0 then
            MsgBox (0,"Error","Can not open ini file")
        Else
            If StringRight (GuiCtrlRead ($Input_1),3) <> "ini" then 
                MsgBox (0,"Error", "File must be an ini file")
                Else
            SortNow(GuiCtrlRead ($Input_1))
            Endif
        EndIf
        
    Case Else
    ;;;
    EndSelect
WEnd
Exit


Func SortNow($ini)
    If FileExists ($ini &".tmp") then FileDelete($ini & ".tmp")
        
$aSArray = IniReadSectionNames ($ini)
If GuictrlRead ($tick) = 1 then _ArraySort( $aSArray,0,1)

For $x = 1 to Ubound ($aSArray) -1
    $String = ""
        $avArray = IniReadSection ($ini, $aSArray[$x])
        guiCtrlSetData ($Label,"Checking " & $aSArray[$x])
        
            For $i = 1 to Ubound ($avArray) -1
                $string = $String & $avArray[$i][0] & "=" & $avArray[$i][1] & Chr(01)
            Next

        $aString = StringSplit ($String, Chr(01))

        _ArraySort( $aString,0,1)

        $File = FileOPen ($ini & ".tmp",1)

        FileWriteLine ($File, "[" & $aSArray[$x] & "]")
            For $i = 1 to Ubound ($aString) -1
            FileWriteLine ($File, $aString[$i])
            guiCtrlSetData ($Label,"Writing " & $aString[$i])
        Next
        FileWriteLine ($File, @CRLF & @CRLF)

        FileClose ($File)
    Next
    guiCtrlSetData ($Label,"Sorted!")
    Sleep (1000)
    FileMove ($ini, Stringtrimright ($ini, 4)  & "Backup" & @MDAY & @Mon & @year & "_" & @Hour & @Min & ".ini")
    FileMove ($ini & ".tmp", $ini)
    MsgBox(0,"Done","Old file has been renamed " & Stringtrimright ($ini, 4)  & "Backup" & @MDAY & @Mon & @year & "_" & @Hour & @Min &".ini")
Run ("notepad " & $ini)
EndFunc
Link to comment
Share on other sites

  • Moderators

Ha, I like the button to activate!!

Are congratulations in order, I heard a rumor of tiny feet?

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

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...