Jump to content

_ArraySearch and multiple resultsets


Recommended Posts

Hey,

This is a function from a tool i am working on. Most variables are named poorly or are Dutch.

What this function does, basicly:

It searches for *.ini files. These are in $datadir = @Scriptdir & "\data". Vestiging is given (for example "Amersfoort" (folder @scriptdir\data\amersfoort) and then the *.ini files. It finds the files, but then it gets a bit harder.

I have tried a lot, while loops, for loops etc but i keep getting the first result only, while i want all the results.

An example of my ini file:

[FastEthernet0/1]
Patch=003-001
Kamer=03-02
[FastEthernet0/2]
Kamer=03-02
Patch=PATCHTEST
[FastEthernet0/3]
Kamer=03-03
Patch=PATCHTEST

So when i search for "PATCHTEST" in "Amersfoort" (_Findpatch("Amersfoort","PATCHTEST") it only gives back the first one, being FastEthernet0/2.

How can i get this to find all of the occurrence's of "Patchtest" in the current ini file it is reading in. It should go to the next one and the next one untill all of the occurrence's from "Patchtest" are found in all the ini files.

Shouldn't be so hard but i can't get it to work properly.

Here is the code i am currently using:

Func _FindPatch($vestiging, $findPatch)
    $search = FileFindFirstFile($datadir & "\" & $vestiging & "\*.ini")

    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Return False
    EndIf
    _GUICtrlListViewDeleteAllItems($ResultView)
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $tidyfile = StringSplit($file, ".")
        $tidy = $tidyfile[1]

        $fileINCPATH = $datadir & "\" & $vestiging & "\" & $file
        _FileReadToArray($fileINCPATH, $taRR)
        $result = _ArraySearch($taRR, "Patch=" & $findPatch)
        If ($result > -1) Then
            $mfil = FileOpen($fileINCPATH, 0)
            $data = FileReadLine($mfil, ($result - 1))
            $patchResult = $data
            If (StringInStr($data, "Kamer=")) Then
                $data = FileReadLine($mfil, ($result - 2))
                $patchResult = $data
            EndIf
            FileClose($mfil)
            $tidyfile = StringSplit($file, ".")
            $tidy = $tidyfile[1]

            $prt = StringSplit($patchResult, "[")
            $prt = StringSplit($prt[2], "]")
            $p1 = $prt[1] ;poort
            $p2 = $tidy ;switch
            $p3 = $vestiging ;vestiging
            $p4 = "-" ;omschrijving
            $p5 = "-" ;status
            $p6 = "-" ;snelheid
            $p7 = "-" ;vlan
            $p8 = _getPatchPort($p2, $p3, $p1) ;patchpoort
            $p9 = _getKamer2($p2, $p3, $p1) ;kamer
            GUICtrlCreateListViewItem($p1 & "|" & $p2 & "|" & $p3 & "|" & $p4 & "|" & $p5 & "|" & $p6 & "|" & $p7 & "|" & $p8 & "|" & $p9, $ResultView)
        Else
            ConsoleWrite("+>LoopC")
        EndIf

    WEnd
    GUICtrlSetState($ResultView, $GUI_SHOW)
    FileClose($search)
EndFunc   ;==>_FindPatch

Thanks in advance!

Link to comment
Share on other sites

  • Developers

Pretty please with sprinkles on top? :whistle:

Haven't tested it but maybe this gives you an idea how to move forward .....

Func _FindPatch($vestiging, $findPatch)
    $search = FileFindFirstFile($datadir & "\" & $vestiging & "\*.ini")

    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Return False
    EndIf
    _GUICtrlListViewDeleteAllItems($ResultView)
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $tidyfile = StringSplit($file, ".")
        $tidy = $tidyfile[1]
        $Ini_sec = IniReadSectionNames($file)
        If @error Then 
            MsgBox(4096, "", "Error occured, probably no INI file.")
        Else
            For $i = 1 To $Ini_sec[0]
                If IniRead($file,$Ini_sec[$i],"Patch","") = $findPatch Then
                    ; *** Patch gevonden ****
                EndIf
            Next
        EndIf
    WEnd
    GUICtrlSetState($ResultView, $GUI_SHOW)
    FileClose($search)
EndFunc   ;==>_FindPatch

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thanks Jos!

I ended up modifying it a bit:

Func _FindPatch($vestiging, $findPatch)
    $search = FileFindFirstFile($datadir & "\" & $vestiging & "\*.ini")

    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Return False
    EndIf
    _GUICtrlListViewDeleteAllItems($ResultView)
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $tidyfile = StringSplit($file, ".")
        $tidy = $tidyfile[1]
        $fileINCPATH = $datadir & "\" & $vestiging & "\" & $file
        $Ini_sec = IniReadSectionNames($fileINCPATH)
        If @error Then
;~             MsgBox(4096, "", "Error occured, probably no INI file. ( " & $fileINCPATH & ")")
            ContinueLoop
        Else
            For $i = 1 To $Ini_sec[0]
                If IniRead($fileINCPATH, $Ini_sec[$i], "Patch", "") = $findPatch Then
                    ; *** Patch gevonden ****
                    MsgBox(0, "", "Found --> " & $Ini_sec[$i])
;~
                    $p1 = $Ini_sec[$i] ;poort
                    $p2 = $tidy ;switch
                    $p3 = $vestiging ;vestiging
                    $p4 = "-" ;omschrijving
                    $p5 = "-" ;status
                    $p6 = "-" ;snelheid
                    $p7 = "-" ;vlan
                    $p8 = _getPatchPort($p2, $p3, $p1) ;patchpoort
                    $p9 = _getKamer2($p2, $p3, $p1) ;kamer
                    GUICtrlCreateListViewItem($p1 & "|" & $p2 & "|" & $p3 & "|" & $p4 & "|" & $p5 & "|" & $p6 & "|" & $p7 & "|" & $p8 & "|" & $p9, $ResultView)
;~
                EndIf
            Next
        EndIf
    WEnd
    GUICtrlSetState($ResultView, $GUI_SHOW)
    FileClose($search)
EndFunc   ;==>_FindPatch

So Thanks again!

Edited by Sypher
Link to comment
Share on other sites

  • Developers

Thanks Jos!

The only thing i'm getting is "Error occured, probably no INI file"

edit:

Okay, it never finds any of the items now :/

The INI fuctions need the full path because the files aren't located in the scriptdir ...

Func _FindPatch($vestiging, $findPatch)
    $search = FileFindFirstFile($datadir & "\" & $vestiging & "\*.ini")

    If $search = -1 Then
        MsgBox(0, "Error", "No files/directories matched the search pattern")
        Return False
    EndIf
    _GUICtrlListViewDeleteAllItems($ResultView)
    While 1
        $file = FileFindNextFile($search)
        If @error Then ExitLoop
        $tidyfile = StringSplit($file, ".")
        $tidy = $tidyfile[1]
        $Ini_sec = IniReadSectionNames($datadir & "\" & $vestiging & "\" & $file)
        If @error Then 
            MsgBox(4096, "", "Error occured, probably no INI file.")
        Else
            For $i = 1 To $Ini_sec[0]
                If IniRead($datadir & "\" & $vestiging & "\" & $file,$Ini_sec[$i],"Patch","") = $findPatch Then
                    ; *** Patch gevonden ****
                EndIf
            Next
        EndIf
    WEnd
    GUICtrlSetState($ResultView, $GUI_SHOW)
    FileClose($search)
EndFunc   ;==>_FindPatch

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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