Jump to content

How do I search for a word inside INI file?


aa2zz6
 Share

Recommended Posts

How do I setup IniRead to search for Cat and write MsgBox($MB_SYSTEMMODAL, "Search", "The" & $animal & "was found at" & $number, 5)

[Search Animals]

IGNORE_ERRORS=0

01=Dog
02=Cat
03=Bird
04=Fish

This displays each key and value but how do I stop it when it gets to 02=Cat?

_read()

Func _read()
    Local Const $hostfile = @ScriptDir & "\filepath\" & "Search.ini"
    $var = IniReadSection($hostfile, "Section Header")
    If @error Then
        MsgBox(4096, "Error", "Unable to read section.")
    Else
        For $i = 1 To $var[0][0]
            MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$i][1])
        Next
        ; How does it stop when Cat is found?
    EndIf
EndFunc   ;==>+read

 

Edited by aa2zz6
Link to comment
Share on other sites

Yes an ExitLoop with a working example.

#include <MsgBoxConstants.au3>
#include <Array.au3>

; -------- Create ini file -----------
FileWrite("Search.ini", _
        "[Search Animals]" & @CRLF & _
        "IGNORE_ERRORS=0" & @CRLF & _
        "01=Dog" & @CRLF & _
        "02=Cat" & @CRLF & _
        "03=Bird" & @CRLF & _
        "04=Fish")
Sleep(1000)
; ------ End of Create ini file ------

_read()

FileDelete("Search.ini") ; Tidy up


Func _read()
    Local Const $hostfile = "Search.ini"
    $var = IniReadSection($hostfile, "Search Animals")
    If @error Then
        MsgBox(4096, "Error", "Unable to read section.")
    Else
        For $number = 1 To $var[0][0]
            ; MsgBox(4096, "", "Key: " & $var[$i][0] & @CRLF & "Value: " & $var[$number][1])
            If $var[$number][1] == "Cat" Then
                MsgBox($MB_SYSTEMMODAL, "Search", "The " & $var[$number][1] & " was found at " & $var[$number][0], 5)
                ExitLoop ; Exits For - Next loop (stops) when search value, "Cat", is found?
            EndIf
        Next
    EndIf
    _ArrayDisplay($var, "Search Animals")
EndFunc   ;==>_read

 

Link to comment
Share on other sites

You could use _ArraySearch to receive the index or _ArrayFindall to find all indexes

_ArraySearch example:

#include <Array.au3>
Global $aResult[0][2]

Search('Cat')

_ArrayDisplay($aResult)

Func Search($sSearch)
    Local $hostfile = @ScriptDir & '\Search.ini'
    Local $aSection = IniReadSection($hostfile, "Search Animals")
    If @error Then
        MsgBox(4096, "Error", "Unable to read section.")
        Return
    EndIf
    Local $iSearch = _ArraySearch($aSection, $sSearch, 0, 0, 0, 0, 1, 1)
    If $iSearch = -1 Then
        MsgBox(4096, 'Error', 'Unable to find ' & $sSearch)
        Return
    EndIf
    _ArrayAdd($aResult, $sSearch & '|' & $iSearch)
EndFunc

 

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