Sign in to follow this  
Followers 0
igorm

Ini file search

5 posts in this topic

Hi,

I need a help with searching .ini file. I need to search for specific key value and as return I need name of the section where value is located.

Thanks in advance for any help.

Cheers :)

Share this post


Link to post
Share on other sites



$result = SearchINI("test.ini", "PASSWORD", true, 2, 0)
MsgBox(0,"",$result)

;FileName = Path to ini file
;ExactMatch = true - match exact string, false - match partial
;Type = Type of search: 0 = Keys and Values, 1 = Keys only, 2 = Values only (default)
;Return = Return type: 0 = String containing section name (default), 1 = true/false
Func SearchINI($sFileName, $sSearchString, $fExactMatch = false, $iType = 2, $iReturn = 0)
    
    $sMatchingSection = ""

    ;Retrieve section names
    $aSectionNames = IniReadSectionNames($sFileName)
    
    ;Loop through all sections
    For $X = 1 to $aSectionNames[0]
        
        ;Retrieve section data
        $aSectionData = IniReadSection ($sFileName, $aSectionNames[$X])
        
        ;Loop through all keys/values in section
        For $Y = 1 to $aSectionData[0][0]
            
            ;Search keys only
            If $iType <> 2 Then
                If $fExactMatch Then
                    If $aSectionData[$Y][0] = $sSearchString Then $sMatchingSection = $aSectionNames[$X]
                Else
                    If StringInStr($aSectionData[$Y][0], $sSearchString) Then $sMatchingSection = $aSectionNames[$X]
                EndIf
            EndIf           
            
            ;Search values only
            If $iType <> 1 Then
                If $fExactMatch Then
                    If $aSectionData[$Y][1] = $sSearchString Then $sMatchingSection = $aSectionNames[$X]
                Else
                    If StringInStr($aSectionData[$Y][1], $sSearchString) Then $sMatchingSection = $aSectionNames[$X]
                EndIf
            EndIf           
        Next
    Next
    
    ;Return specified type
    If $iReturn = 0 Then
        Return $sMatchingSection
    Else
        If $sMatchingSection <> "" Then 
            Return True
        Else
            Return False
        EndIf
    EndIf
EndFunc

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I like weaponx's, this could also be modded to return "all" sections if someone put forth a little effort:

MsgBox(64, "Info", _StringGetIniSection(@DesktopDir & "\myTest.ini", "8", 1))
Func _StringGetIniSection($hIni, $sFind, $vExact = ".*?")
    If $vExact Then $vExact = ""
    Local $aSRE = StringRegExp(@LF & FileRead($hIni), _
        "(?s)(?i)\n\s*\[\s*(.*?)\s*\]\s*\r\n.*?=" & $vExact & $sFind & $vExact & "(?m:$|\[|\r\n)", 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    Return $aSRE[0]
EndFuncoÝ÷ ØGb´-¹©eȬ¶·­º¹âayªëk$^¶êçÓ~I,¥v·­º¹ÚZrêëzw±*"Ë^iÚ¶nÞʦm)¶hjëh×6Func _StringGetIniSection($hIni, $sFind, $vExact = ".*?", $nReturnAll = 0)
    If $vExact Then $vExact = ""
    Local $aSRE = StringRegExp(@LF & FileRead($hIni), _
        "(?s)(?i)\n\s*\[\s*(.*?)\s*\]\s*\r\n.*?=" & $vExact & $sFind & $vExact & "(?m:$|\[|\r\n)", 3)
    If IsArray($aSRE) = 0 Then Return SetError(1, 0, "")
    If $nReturnAll Then Return $aSRE
    Return $aSRE[0]
EndFunc

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.

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