Jump to content

Sort a .ini?


Lemures
 Share

Recommended Posts

I'm very tired and simply dont feel like doing this at the moment. Anybody have any tricks up their sleeve for doing the following?

I have a .ini file with a lot of sections, and I need to reorganize the .ini file so that the sections and their keys are put in it alphabetically in descending order. I have another program that reads through the sections, but when they are out of order it gets frustrating. Any ideas?

Link to comment
Share on other sites

I'm very tired and simply dont feel like doing this at the moment. Anybody have any tricks up their sleeve for doing the following?

I have a .ini file with a lot of sections, and I need to reorganize the .ini file so that the sections and their keys are put in it alphabetically in descending order. I have another program that reads through the sections, but when they are out of order it gets frustrating. Any ideas?

I would recommend you use the INIReadSectionNames() to create an array of all the section names. Use _ArraySort() (standard include) to sort them how you want them. Use INIWrite() with INIReadSection() using the values in the INIReadSectionNames() array you previously created, and write a new sorted INI file. Delete the old file, and rename the new file with a FileMove().

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

I would recommend you use the INIReadSectionNames() to create an array of all the section names. Use _ArraySort() (standard include) to sort them how you want them. Use INIWrite() with INIReadSection() using the values in the INIReadSectionNames() array you previously created, and write a new sorted INI file. Delete the old file, and rename the new file with a FileMove().

JS

sounds like a plan. as soon as i wake up tomorrow morning, I will do it :lmao: Thanks!

By the way, the program is for EVE online, if anybody plays that, ive got some pretty need programs ive made that make working the market ingame a LOT easier.

Link to comment
Share on other sites

sounds like a plan. as soon as i wake up tomorrow morning, I will do it :lmao: Thanks!

By the way, the program is for EVE online, if anybody plays that, ive got some pretty need programs ive made that make working the market ingame a LOT easier.

No problem. Get some sleep, and no doubt tomorrow you will be fired up and ready to roll! I am happy I was able to help you in the right direction.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

  • Moderators

This seemed it could be useful, so I gave it a go:

[zorder]

b=1

d=3

c=4

a=g

[corder]

g=6

k=3

d=2

z=3

a=2

b=3

Func _IniSort($hIni)
    Local $aIRSN = IniReadSectionNames($hIni)
    If Not IsArray($aIRSN) Then Return SetError(1, 0, 0)
    _ArraySort($aIRSN, 0, 1)
    Local $aKey, $sHold
    For $iCC = 1 To UBound($aIRSN) - 1
        Local $aIRS = IniReadSection($hIni, $aIRSN[$iCC])
        If Not IsArray($aIRS) Then ContinueLoop
        For $xCC = 1 To $aIRS[0][0]
            $aKey &= $aIRS[$xCC][0] & Chr(1)
        Next
        If $aKey Then
            $aKey = StringSplit(StringTrimRight($aKey, 1), Chr(1))
            _ArraySort($aKey, 0, 1)
            $sHold &= '[' & $aIRSN[$iCC] & ']' & @CRLF
            For $aCC = 1 To UBound($aKey) - 1
                $sHold &= $aKey[$aCC] & '=' & IniRead($hIni, $aIRSN[$iCC], $aKey[$aCC], 'blahblah') & @CRLF
            Next
            $aKey = ''
        EndIf
    Next
    If $sHold Then
        $sHold = StringTrimRight($sHold, 2)
        FileClose(FileOpen($hIni, 2))
        FileWrite($hIni, $sHold)
        Return 1
    EndIf
    Return SetError(1, 0, 0)
EndFuncoÝ÷ ØGb´èXy¨jº^
Would have saved alot of time if I had looked :lmao: Edited by SmOke_N

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 would recommend you use the INIReadSectionNames() to create an array of all the section names. Use _ArraySort() (standard include) to sort them how you want them. Use INIWrite() with INIReadSection() using the values in the INIReadSectionNames() array you previously created, and write a new sorted INI file. Delete the old file, and rename the new file with a FileMove().

JS

I will try this too. Thanks for the guidance.

Link to comment
Share on other sites

I will try this too. Thanks for the guidance.

NP I hope it works out for you.

JS

AutoIt Links

File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out.

ComputerGetInfo UDF's Updated! 11-23-2006

External Links

Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more)

Link to comment
Share on other sites

This should be trivial. Start by renaming the source file to move it out of the way. Now read the section names from the source with IniReadSectionNames() and use _ArraySort() on it. Now you know which order to process the sections in. Now iterate that array using InIReadSection() to read the section and use _ArraySort() on that 2D array to sort it (by key). Now use IniWriteSection() (*Beta-only*) to write the section to a new file. Lastly, delete the source file. Be aware that IniReadSection() strips any non key=value lines so comments or invalid syntax will be stripped. It should be about half the code Smoke shows.

Link to comment
Share on other sites

This is one I did some time ago if it helps.

#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

  • 6 years later...

This seemed it could be useful, so I gave it a go:

[autoit]Func _IniSort($hIni)

Local $aIRSN = IniReadSectionNames($hIni)

If Not IsArray($aIRSN) Then Return SetError(1, 0, 0)

_ArraySort($aIRSN, 0, 1)

Local $aKey, $sHold

For $iCC = 1 To UBound($aIRSN) - 1

Local $aIRS = IniReadSection($hIni, $aIRSN[$iCC])

If Not IsArray($aIRS) Then ContinueLoop

For $xCC = 1 To $aIRS[0][0]

$aKey &= $aIRS[$xCC][0] & Chr(1)

Next

If $aKey Then

$aKey = StringSplit(StringTrimRight($aKey, 1), Chr(1))

_ArraySort($aKey, 0, 1)

$sHold &= '[' & $aIRSN[$iCC] & ']' & @CRLF

For $aCC = 1 To UBound($aKey) - 1

$sHold &= $aKey[$aCC] & '=' & IniRead($hIni, $aIRSN[$iCC], $aKey[$aCC], 'blahblah') & @CRLF

Next

$aKey = ''

EndIf

Next

If $sHold Then

$sHold = StringTrimRight($sHold, 2)

FileClose(FileOpen($hIni, 2))

FileWrite($hIni, $sHold)

Return 1

EndIf

Return SetError(1, 0, 0)

EndFunc

Link to comment
Share on other sites

  • Moderators

gcue,

Did you really mean to post in thread that was over 6 1/2 years old? :huh:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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