Sign in to follow this  
Followers 0
d0n

Sorting ini or txt files

15 posts in this topic

is there a way to sort ini files, i only found functions to sort GUI stuff

my file is similar to this, and i want to be able to sort them ascending or decending

Me=123

Jon=412

Dave=12

what kind of functions should i be looking at to achieve this?

Share this post


Link to post
Share on other sites



_IniSort()

http://dundats.mvps.org/AutoIt/udf_code.aspx?udf=ini


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

that answers your question anyway, but you can also put the ini data in an array, then sort the array and finally write back into the ini file.

Share this post


Link to post
Share on other sites

I got this error when using the _IniSortSectionByValue()

C:\Program Files\AutoIt3\Include\ini.au3 (101) : ==> Incorrect number of parameters in function call.:

_ArraySort($sArray, $s_Order, 1, 0, 2, 1)

^ ERROR

->12:29:29 AutoIT3.exe ended.rc:1

+>12:29:30 AutoIt3Wrapper Finished

>Exit code: 1 Time: 5.079

#Include <File.au3>
#include <ini.au3>
$Log = @ScriptDir&"\Logger.ini"

If FileExists($Log) = 0 Then
    _FileCreate($Log)
    IniWrite($Log, "Name", "####", "0")
EndIf

$sStr = FileRead(FileOpenDialog("Select the Log file", @Scriptdir&"\","Text (*.txt)"))

;Dim $sStr = FileRead(@scriptdir & "\test.txt")

Dim $sPatt = '(?i)\[[^]]*\]\s*(.*?)(?:hit|slashe?)s?.*?for\s*(\d+)'
Dim $aLines = StringSplit($sStr, @CRLF, 1)

Global $P1_Total = 0

For $i = 1 To $aLines[0]
    Local $aMatch = StringRegExp($aLines[$i], $sPatt, 1)
If UBound($aMatch) = 2 Then
        $a = IniReadSection($Log, "Name")
        IniDelete($Log, "Name", "####")
        $aMatch[0] = StringTrimRight($aMatch[0],1)
        For $size=1 To $a[0][0]
            If $a[$size][0] = $aMatch[0] Then
                $dmg = IniRead($Log, "Name", $aMatch[0], "0")
                $dmg = $dmg + $aMatch[1]
                IniWrite($Log, "Name", $aMatch[0], $dmg)
                exitloop
            EndIf
        Next
        If $size >  $a[0][0] then  IniWrite($Log, "Name", $aMatch[0], $aMatch[1])
    EndIf
Next

_IniSortSectionByValue($Log, "Name", 0)

Share this post


Link to post
Share on other sites

I got this error when using the _IniSortSectionByValue()

#Include <File.au3>
#include <ini.au3>
$Log = @ScriptDir&"\Logger.ini"

If FileExists($Log) = 0 Then
    _FileCreate($Log)
    IniWrite($Log, "Name", "####", "0")
EndIf

$sStr = FileRead(FileOpenDialog("Select the Log file", @Scriptdir&"\","Text (*.txt)"))

;Dim $sStr = FileRead(@scriptdir & "\test.txt")

Dim $sPatt = '(?i)\[[^]]*\]\s*(.*?)(?:hit|slashe?)s?.*?for\s*(\d+)'
Dim $aLines = StringSplit($sStr, @CRLF, 1)

Global $P1_Total = 0

For $i = 1 To $aLines[0]
    Local $aMatch = StringRegExp($aLines[$i], $sPatt, 1)
If UBound($aMatch) = 2 Then
        $a = IniReadSection($Log, "Name")
        IniDelete($Log, "Name", "####")
        $aMatch[0] = StringTrimRight($aMatch[0],1)
        For $size=1 To $a[0][0]
            If $a[$size][0] = $aMatch[0] Then
                $dmg = IniRead($Log, "Name", $aMatch[0], "0")
                $dmg = $dmg + $aMatch[1]
                IniWrite($Log, "Name", $aMatch[0], $dmg)
                exitloop
            EndIf
        Next
        If $size >  $a[0][0] then  IniWrite($Log, "Name", $aMatch[0], $aMatch[1])
    EndIf
Next

_IniSortSectionByValue($Log, "Name", 0)

I'll fix that in a few minutes. I forgot to change the upload the last time. I'll send you a PM when it's ready which will be right after I re-test it.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Thank you! :)

Share this post


Link to post
Share on other sites

Now I remember why I didn't upload the new version, I shave a couple more functions to add to it.

Change the defective line to _ArraySort($sArray, $s_Order, 1, 0, 2) and it should work. I'm still doing more testing before I do the upload.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

No errors this time but it still didn't sort the ini file

The code

#include <ini.au3>
$Log = @ScriptDir&"\Logger.ini"
_IniSortSectionByValue($Log, "Name")

The Ini file

[Name]
Don=123
Jon=321
Jack=23

Share this post


Link to post
Share on other sites

No errors this time but it still didn't sort the ini file

The code

#include <ini.au3>
$Log = @ScriptDir&"\Logger.ini"
_IniSortSectionByValue($Log, "Name")

The Ini file

[Name]
Don=123
Jon=321
Jack=23

That would be becuase I gave you the wrong code.

This works for me

_ArraySort($sArray, $s_Order, 1, 0, 1)


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

ok, it works but i have a question

right now its sorting it a different way than i expected

Don=123
Jon=321
Jack=23

sorts into

Don=123
Jack=23
Jon=321

but i was hoping that it would turn into

Jack=23
Don=123
Jon=321

Since 23<123<321

Share this post


Link to post
Share on other sites

#include <Array.au3>

Dim $sFile = @ScriptDir & '\file.ini'
Dim $aINI = IniReadSection($sFile, 'name')

_ArrayDisplay($aINI)
For $i = 1 To $aINI[0][0]
    For $j = 1 To $aINI[0][0]-$i
        If Int($aINI[$j+1][1]) < Int($aINI[$j][1]) Then
            Local $sTemp = $aINI[$j][1]
            $aINI[$j][1] = $aINI[$j+1][1]
            $aINI[$j+1][1] = $sTemp
        EndIf
    Next
Next
_ArrayDisplay($aINI)

Share this post


Link to post
Share on other sites

that sorts the value in col1 but the name corresponding to the value doesn't shift with it after sorting ending up with a wrong value for some names

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

#include <Array.au3>

Dim $sFile = @ScriptDir & '\file.ini'
Dim $aINI = IniReadSection($sFile, 'name')

_ArrayDisplay($aINI)
For $i = 1 To $aINI[0][0]
    For $j = 1 To $aINI[0][0]-$i
        If Int($aINI[$j+1][1]) < Int($aINI[$j][1]) Then
            Local $sTemp = $aINI[$j][1]
            $aINI[$j][1] = $aINI[$j+1][1]
            $aINI[$j+1][1] = $sTemp
        EndIf
    Next
Next
_ArrayDisplay($aINI)

I knew that was out there someplace. I'm going to have to modify that function to handle numeric values one of these days. I'll put it on the list of things to do tomorrow and we know the story about that day. Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

@d0n: A little bit modification is required to swap names as well, the concept is similar. Just store the element of the array you want to replace, move the next element the current element and put the stored value in the next element. About 4 more lines of code. :)

I'm working on a quick sort with a recursive function all done using Execute() function to get the value of a variant array to an arbitrary dimensional length but it trick is to find a way to store it this way. More testing.

Share this post


Link to post
Share on other sites

Worked with 3 more lines :), thanks for the help

#include <Array.au3>

Dim $sFile = @ScriptDir & '\RevLogger.ini'
Dim $aINI = IniReadSection($sFile, 'name')

_ArrayDisplay($aINI)
For $i = 1 To $aINI[0][0]
    For $j = 1 To $aINI[0][0]-$i
        If Int($aINI[$j+1][1]) > Int($aINI[$j][1]) Then
            Local $sTemp = $aINI[$j][1]
            $aINI[$j][1] = $aINI[$j+1][1]
            $aINI[$j+1][1] = $sTemp
        Local $sTemp1 = $aINI[$j][0]
            $aINI[$j][0] = $aINI[$j+1][0]
            $aINI[$j+1][0] = $sTemp1
        EndIf
    Next
Next
_ArrayDisplay($aINI)

Share this post


Link to post
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
Sign in to follow this  
Followers 0