Sort INI FILE Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Sort INI FILE

Post by HiSoKa » 29 Nov 2022, 01:30

Hello,
How i can sort INI File and retrieve the sections according to values which in it.
This INI File:
[MySection 1]
pos=2

[MySection 2]
pos=1

[MySection 3]
pos=5

[MySection 4]
pos=3

[MySection 5]
pos=4


The desired result should be as follows:
[MySection 2]
[MySection 1]
[MySection 4]
[MySection 5]
[MySection 3]

Rohwedder
Posts: 7645
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Sort INI FILE  Topic is solved

Post by Rohwedder » 29 Nov 2022, 02:23

Hallo,
try:

Code: Select all

INI =
(
[MySection 1]
pos=2

[MySection 2]
pos=1

[MySection 3]
pos=5

[MySection 4]
pos=3

[MySection 5]
pos=4
)
Loop, Parse,% (StrReplace(INI, "`npos"), INI:=""), `n
	IF RegExMatch(A_LoopField, "([^=]+)=(.*)", M)
		INI .= M2 M1 "`n"
Sort, INI, N
MsgBox,% INI := RegExReplace(INI, "`nm)^\d+")
Last edited by Rohwedder on 29 Nov 2022, 02:42, edited 1 time in total.

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Sort INI FILE

Post by HiSoKa » 29 Nov 2022, 02:40

Thanks you @Rohwedder :salute:

User avatar
Xeo786
Posts: 760
Joined: 09 Nov 2015, 02:43
Location: Karachi, Pakistan

Re: Sort INI FILE

Post by Xeo786 » 29 Nov 2022, 04:07

You can also use initools lib

Code: Select all

Array2INI(INI2Array(input_ini), output_ini) ;this will sort and remove comments 
"When there is no gravity, there is absolute vacuum and light travel with no time" -Game changer theory

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Sort INI FILE

Post by HiSoKa » 29 Nov 2022, 05:26

Xeo786 wrote:
29 Nov 2022, 04:07
You can also use initools lib

Code: Select all

Array2INI(INI2Array(input_ini), output_ini) ;this will sort and remove comments 
Thanks. Your library will be very useful to me..

just me
Posts: 9458
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Sort INI FILE

Post by just me » 29 Nov 2022, 06:33

Hi @HiSoKa,

if it's possible that the pos key won't always immediately follow the section header I'd prefer the following:

Code: Select all

#NoEnv
/* INI
[MySection 1]
pos=2

[MySection 2]
pos=1

[MySection 3]
pos=5

[MySection 4]
pos=3

[MySection 5]
pos=4
[]
*/
IniFile := A_ScriptFullPath
IniRead, Sections, %IniFile%
Sorted := ""
For Each, Section In StrSplit(Sections, "`n") {
   IniRead, Pos, %IniFile%, %Section%, Pos
   Sorted .= (Pos != "ERROR") ? Pos . Section . "`n" : ""
}
Sort, Sorted, N
Sorted := RegExReplace(Trim(Sorted, "`n"), "`nm)^\d+")
MsgBox, %Sorted%

User avatar
HiSoKa
Posts: 480
Joined: 27 Jan 2020, 15:43

Re: Sort INI FILE

Post by HiSoKa » 29 Nov 2022, 08:25

yes, pos key don't always immediately follow the section header,
And Your code more readable for me :mrgreen: ,, Thanks you so much @just me

Post Reply

Return to “Ask for Help (v1)”