Jump to content

Ini file search


 Share

Recommended Posts

$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

Link to comment
Share on other sites

  • Moderators

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.

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