Autohotkey-反向排序 ini 文件?

Autohotkey-反向排序 ini 文件?

是否有可能反向读取 ini 文件?

可以用“sort”或“filereadline”循环来完成吗?

配置文件

[section1]
key1=1
[section2]
key2=2
[section3]
key3=3
[section4]
key4=4

代码:

FileRead, file, a.ini
Gui, Font, s14 cBlue
Gui Add, Edit, w435 h400 vfile, %file%
Gui, show

我想要的结果:

[section4]
key4=4
[section3]
key3=3
[section2]
key2=2
[section1]
key1=1

答案1

; Read in a list of the section names
IniRead,sectionsString,a.ini

; Split the list into an array
; The 0th term will contain the count of how many items there are in the array
StringSplit,sectionsArray,sectionsString,`n

; Read each section in reverse order and compile the results
reversedIni=
Loop, %sectionsArray0%
{
    ; We want to count from end of the list to the beginning so adjust based on A_Index
    thisSectionNumber := sectionsArray0 - A_Index + 1
    
    ; Pull the name of this section
    thisSection := sectionsArray%thisSectionNumber%
    
    ; Read the text from this section
    IniRead,sectionText,a.ini,%thisSection%
    
    ; Format the results and add it to the reversed INI we're building
    reversedIni := reversedIni . "[" . thisSection . "]`n" . sectionText . "`n"
}

; Display the result
Msgbox % reversedIni

在此处输入图片描述

答案2

Loop, read, a.ini  ; retrieves the lines in this file, one at a time
{
    If (SubStr(A_LoopReadLine, 1, 1) = "[") ; section
        content .= "`n" A_LoopReadLine ; concatenate the outputs by adding a linefeed before each one
    else ; key
        content .= A_Space A_LoopReadLine ; concatenate the outputs by adding a space before each one
}
; MsgBox, %content%
Sort, content, R ; sort the concatenated lines in reverse order
; MsgBox, %content%
Loop, parse, content, `n ; split by linefeed
{
    If (A_LoopField = "") ; ignore empty lines
       continue
    LoopField := StrReplace(A_LoopField, A_Space, "`n") ; replace spaces by linefeeds
    result .= "`n" LoopField ; concatenate the outputs
}
result := Trim(result, "`n") ; remove leading linefeeds 
MsgBox, %result%

相关内容