Jump to content

Sort ini file numerically


memerim
 Share

Recommended Posts

How to sort in ascending order an .ini, and save to the original file, like:

This:

[COLOURS]
8=blue
12=green
17=green
118=purple
19=blue
112=white
31=white
39=yellow
113=white
23=green
45=green
71=white
114=white
14=blue
73=purple
60=red
66=yellow
98=green
40=violet
42=purple
68=blue

To:

[COLOURS]
8=blue
12=green
14=blue
17=green
19=blue
23=green
31=white
39=yellow
40=violet
42=purple
45=green
60=red
66=yellow
68=blue
71=white
73=purple
98=green
112=white
113=white
114=white
118=purple

 

I've searched some similar topics;

I tried adapt this script from @Nine

Got error, and my doubt is how to save to the original file instead display a array.

#include <File.au3>
#include <Array.au3>

Opt ("MustDeclareVars", 1)

Local $array
_FileReadToArray ("C:\ProgramData\colours.ini", $array, $FRTA_ENTIRESPLIT, "=")
For $i = 0 to UBound ($array)-1
  $array[$i][1]=Number($array[$i][1])
Next
_ArraySort ($array, 0, 0, 0, 1)

Local $value = $array[0][1], $start = 0
For $i = 1 to UBound ($array)-1
  if $value = $array[$i][1] then ContinueLoop
  $value = $array[$i][1]
  if $i - $start > 1 then _ArraySort ($array, 0, $start, $i-1, 0)
  $start = $i
Next
if UBound ($array) - $start > 1 then _ArraySort ($array, 0, $start, Ubound($array)-1, 0)

_ArrayDisplay ($array)

Local $value = $array[0][1], $start = 8
Local $value = $array^ ERROR

Edited by memerim
Link to comment
Share on other sites

  • Developers

Something like this should be close: (make sure you have a backup before testing!)

#include <Array.au3>
$sFilePath = "colours.ini"
; Read the INI section labelled 'General'. This will return a 2 dimensional array.
Local $aArray = IniReadSection($sFilePath, "COLOURS")
If @error Then Exit
; process data from section
For $i = 1 To $aArray[0][0]
    ; convert KEY string to value
    $aArray[$i][0] = number($aArray[$i][0])
    consolewrite("Key: " & $aArray[$i][0] & "    Value: " & $aArray[$i][1] & @CRLF )
Next
_ArraySort($aArray,0,1)
; Remove old unsorted Section from INI
IniDelete($sFilePath,"COLOURS")
; Write sorted values
For $i = 1 To $aArray[0][0]
    IniWrite($sFilePath,"COLOURS",$aArray[$i][0],$aArray[$i][1])
    consolewrite("Key: " & $aArray[$i][0] & "    Value: " & $aArray[$i][1] & @CRLF )
Next

Jos :) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

It would be interesting to know why you need to sort an Ini file at all? The sequence does not matter when using AutoIt supplied INI* functions.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

I needed something to do the same a while ago. Ended up with something similar to Jos.

#include <Array.au3>
;~ #include <FileConstants.au3>

_Ini_SortSectionByValue('C:\ProgramData\colours.ini', 'COLOURS', 0, 1)
If @error Then MsgBox(0, '', @error)

Func _Ini_SortSectionByValue($s_File, $s_Section, $i_Ascending = 0, $b_KeysAreNumbers = 0)
    Local $a_IniRead

    ; choose sort order
    If $i_Ascending <> 0 Then $i_Ascending = 1

    ; read the section
    $a_IniRead = IniReadSection($s_File, $s_Section)
    If @error Then Return SetError(@error)

    If $b_KeysAreNumbers Then
        For $i = 1 To $a_IniRead[0][0]
            $a_IniRead[$i][0] = Number($a_IniRead[$i][0])
            $a_IniRead[$i][1] = $a_IniRead[$i][1]
        Next
    EndIf

    _ArraySort($a_IniRead, $i_Ascending, 1)

    If IsArray($a_IniRead) Then
        ; save orig
;~     FileMove($s_File, $s_File & '.old', BitOR($FC_OVERWRITE, $FC_CREATEPATH))

        ; write section
        IniWriteSection($s_File, $s_Section, $a_IniRead)
        If @error Then Return SetError(@error)
    EndIf

EndFunc   ;==>_Ini_SortSectionByValue

 

Link to comment
Share on other sites

one more

#include<array.au3>
$aIni = IniReadSection("colours.ini", "colours")

For $i = 1 to ubound($aIni) - 1
_ArraySwap($aIni , $i ,  _ArrayMinIndex($aIni , 1 , $i))
Next

_ArrayDisplay($aIni)

 

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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