Jump to content

2 dimensional array


Recommended Posts

Hi,

I'm crafting up a GUI tool that can (re-)parse AWStats logs on a IIS Webserver, eventually in the GUI i wan't to create a Dropdown that show a list with site names, once selected the value from LogfilePath is taken to proceed the actions.

I'm trying to create 2 dimensional array containing

- The Sites name

- The LogfilePath for this specific site

I'm using WMI to grab the sitenames and logfilepath for the specific sites, eg:

#include <Array.au3>

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")

;Get IIS Log Directories
$n = 0
dim $avArray[1]
$colItems = $objWMIService.ExecQuery ("Select * from IIsWebServerSetting")
For $colItem In $colItems
     ; $colItem.ServerComment = Site description
     ; $colItem.LogFileDirectory = Site LogFilePath
    $n += 1
    _ArrayAdd($avArray, $colItem.ServerComment)
    ReDim $avArray[$n]
Next

_ArrayDisplay($avArray, "Array with IIS websites")

No problems creating a single column array as in the example above, but i can not find any straigtforward documentation on how to create a 2 dimensional array holding the $colItem.LogFileDirectory as 2nd parameter.

I have seen some examples, code excerpts that use something like this:

dim $avArray[1][2]

But as it seems i can not use _ArrayAdd command to add the second value, anyone can help me out here?

thx,

RvdH

Edited by RvdH
Link to comment
Share on other sites

Give it a try:

#include <Array.au3>

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")

;Get IIS Log Directories
dim $avArray[1][2]
$colItems = $objWMIService.ExecQuery ("Select * from IIsWebServerSetting")
For $colItem In $colItems
    ReDim $avArray[UBound($avArray) + 1][2]
    $avArray[UBound($avArray) - 1][0] = $colItem.ServerComment
    $avArray[UBound($avArray) - 1][1] = $colItem.LogFileDirectory
Next
$avArray[0][0] = UBound($avArray) - 1

_ArrayDisplay($avArray, "Array with IIS websites")

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

Cool that seems to work, a few additional questions though...

First, How to eliminate the empty array row [0]?

I have got 10 values displayed now, as illustrated below but why does row [0] says 9 in Col 1?

[0] 9 |

[1] domain1 | path1

[2] domain2 | path2

[3] domain3 | path3

[4] domain4 | path4

[5] domain5 | path5

[6] domain6 | path6

[7] domain7 | path7

[8] domain8 | path8

[9] domain9 | path9

Edited by RvdH
Link to comment
Share on other sites

Give it a try:

#include <Array.au3>

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")

;Get IIS Log Directories
dim $avArray[1][2]
$colItems = $objWMIService.ExecQuery ("Select * from IIsWebServerSetting")
For $colItem In $colItems
    ReDim $avArray[UBound($avArray) + 1][2]
    $avArray[UBound($avArray) - 1][0] = $colItem.ServerComment
    $avArray[UBound($avArray) - 1][1] = $colItem.LogFileDirectory
Next
$avArray[0][0] = UBound($avArray) - 1

_ArrayDisplay($avArray, "Array with IIS websites")
This is simpler (though it may not look so because I added error handling):
#include <Array.au3>

Global $avArray[1][2], $objWMIService, $colItems, $oItem, $n

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")
If Not IsObj($objWMIService) Then
    MsgBox(16, "Error", "$objWMIService is not an object.")
    Exit
EndIf

$colItems = $objWMIService.ExecQuery("Select * from IIsWebServerSetting")
If $colItems.count > 0 Then
    ReDim $avArray[$colItems.count + 1][2]
    $avArray[0][0] = $colItems.count
    $n = 1
    For $oItem In $colItems
        $avArray[$n][0] = $oItem.ServerComment
        $avArray[$n][1] = $oItem.LogFileDirectory
        $n += 1
    Next

    _ArrayDisplay($avArray, "Array with IIS websites")
Else
    MsgBox(16, "Error", "Collection contained no objects.")
    Exit
EndIf
The point is that the array is only ReDim'ed once.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Cool that seems to work, a few additional questions though...

First, How to eliminate the empty array row [0]?

I have got 10 values displayed now, as illustrated below but why does row [0] says 9 in Col 1?

I just follow autoit array example. Usually in autoit only row[0] hold the the number of element inside the array.

Try this other one it eliminate the empty array row[0]

#include <Array.au3>

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")

;Get IIS Log Directories
dim $avArray[1][2]
$colItems = $objWMIService.ExecQuery ("Select * from IIsWebServerSetting")
For $colItem In $colItems
    $avArray[UBound($avArray) - 1][0] = $colItem.ServerComment
    $avArray[UBound($avArray) - 1][1] = $colItem.LogFileDirectory
    ReDim $avArray[UBound($avArray) + 1][2]
Next
ReDim $avArray[UBound($avArray) - 1][2]

_ArrayDisplay($avArray, "Array with IIS websites")
AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line
Link to comment
Share on other sites

Yep, that did it... now i just need to take yours and build-in the error handling posted by PsaltyDS :)

I tested the one I posted on one of my servers, and it works without modification. You do get the count in [0][0].

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

yep with yours i also have it returned like:

[0] 9 |

[1] domain1 | path1

[2] domain2 | path2

[3] domain3 | path3

[4] domain4 | path4

[5] domain5 | path5

[6] domain6 | path6

[7] domain7 | path7

[8] domain8 | path8

[9] domain9 | path9

I got it working this way currently:

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")
If Not IsObj($objWMIService) Then
    MsgBox(16, "Error", "$objWMIService is not an object.")
    Exit
EndIf

;Get IIS Log Directories
dim $avArray[1][2]
$colItems = $objWMIService.ExecQuery ("Select * from IIsWebServerSetting")
If $colItems.count > 0 Then
    For $colItem In $colItems
        $avArray[UBound($avArray) - 1][0] = $colItem.ServerComment
        $avArray[UBound($avArray) - 1][1] = $colItem.LogFileDirectory
        ReDim $avArray[UBound($avArray) + 1][2]
    Next
    ReDim $avArray[UBound($avArray) - 1][2]

    _ArrayDisplay($avArray, "Array with IIS websites")
Else
    MsgBox(16, "Error", "Collection contained no objects.")
    Exit
EndIf
Edited by RvdH
Link to comment
Share on other sites

Can i ask you guys help once more,

ok this array thingy is working preety ok now, but now my next dillema,

Now i want to create a GUI with a ComboBox who will display the website names (col1), for the selected site i like to update a variable with the LogFilePath value (col2)

So if "domain 2" is selected i like to set a $logfilepath variable to "path2"

Link to comment
Share on other sites

Can i ask you guys help once more,

ok this array thingy is working preety ok now, but now my next dillema,

Now i want to create a GUI with a ComboBox who will display the website names (col1), for the selected site i like to update a variable with the LogFilePath value (col2)

So if "domain 2" is selected i like to set a $logfilepath variable to "path2"

Take a stab at it and post your code if you get stuck. Keep it simple for a test if you haven't done it before. Just a basic GUI with just the minimum required controls to demonstrate what you want to do.

Start out with the example script in the help file under GuiCtrlCreateCombo().

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

this is what i have right now,

$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")
If Not IsObj($objWMIService) Then
    MsgBox(16, "Error", "$objWMIService is not an object.")
    Exit
EndIf

;Get IIS Log Directories
dim $avArray[1][2]
$colItems = $objWMIService.ExecQuery ("Select * from IIsWebServerSetting")
If $colItems.count > 0 Then
    For $colItem In $colItems
        $avArray[UBound($avArray) - 1][0] = $colItem.ServerComment
        $avArray[UBound($avArray) - 1][1] = $colItem.LogFileDirectory
        ReDim $avArray[UBound($avArray) + 1][2]
    Next
    ReDim $avArray[UBound($avArray) - 1][2]

    ;_ArrayDisplay($avArray, "Array with IIS websites")
    
    GUICreate("My GUI combo")  ; will create a dialog box that when displayed is centered
    GUISetState(@SW_SHOW)
    $Combo = GUICtrlCreateCombo ("", 10,10) ; create first item
    
    For $i = 0 To Ubound($avArray) - 1
        $output = GUICtrlSetData(-1,$avArray[$i][0]) ; add sites
    Next
    
    GUISetState()
    While 1
        $msg = GUIGetMsg()
        Switch $msg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $Combo
                MsgBox(0, "", GUICtrlRead($Combo))
        EndSwitch
    WEnd
Else
    MsgBox(16, "Error", "Collection contained no objects.")
    Exit
EndIf

But how do i get back the value of the selected LogFilePath Array, eg: $avArray[$i][1] instead of the sites name, eg: $avArray[$i][0] ?

Link to comment
Share on other sites

this is what i have right now,

But how do i get back the value of the selected LogFilePath Array, eg: $avArray[$i][1] instead of the sites name, eg: $avArray[$i][0] ?

Study this version for a bit:
#include <GuiConstants.au3>

Global $avArray[1][2], $objWMIService, $colItems, $oItem, $n
Global $hGUI, $ctrlCombo, $ctrlButton, $sData

; Connect to object
$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")
If Not IsObj($objWMIService) Then
    MsgBox(16, "Error", "$objWMIService is not an object.")
    Exit
EndIf

; Query for sites
$colItems = $objWMIService.ExecQuery("Select * from IIsWebServerSetting")
If $colItems.count > 0 Then
; Build array of sites
    ReDim $avArray[$colItems.count + 1][2]
    $avArray[0][0] = $colItems.count
    $n = 1
    For $oItem In $colItems
        $avArray[$n][0] = $oItem.ServerComment
        $avArray[$n][1] = $oItem.LogFileDirectory
        $n += 1
    Next

; Build GUI
    $hGUI = GUICreate("Combo Test", 400, 200)
    $ctrlCombo = GUICtrlCreateCombo($avArray[1][0], 20, 20, 360, 100)
    $ctrlLabel_1 = GUICtrlCreateLabel("Selected Name: ", 20, 60, 360, 20)
    $ctrlLabel_2 = GUICtrlCreateLabel("Selected Path: ", 20, 100, 360, 20)
    $ctrlButton = GUICtrlCreateButton("READ", 150, 150, 100, 30)
    If $avArray[0][0] > 1 Then
        For $n = 2 To $avArray[0][0]
            GUICtrlSetData($ctrlCombo, $avArray[$n][0])
        Next
    EndIf
    GUISetState()
    
; GUI message loop
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $ctrlButton
            ; Read combo and set labels
                $sData = GUICtrlRead($ctrlCombo)
                GUICtrlSetData($ctrlLabel_1, "Seclected Name: " & $sData)
                For $n = 1 To $avArray[0][0]
                ; Find matching entry in array
                    If $avArray[$n][0] = $sData Then
                        GUICtrlSetData($ctrlLabel_2, "Selected Path: " & $avArray[$n][1])
                        ExitLoop
                    EndIf
                Next
        EndSwitch
    WEnd
Else
    MsgBox(16, "Error", "Collection contained no objects.")
    Exit
EndIf

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Awesome, currenly trying to learn what each procedure does but already found out i needed another var "$oItem.Name" to get to the fll path

updated script:

#include <GuiConstants.au3>

Global $avArray[1][3], $objWMIService, $colItems, $oItem, $n
Global $hGUI, $ctrlCombo, $ctrlButton, $sData

; Connect to object
$objWMIService = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")
If Not IsObj($objWMIService) Then
    MsgBox(16, "Error", "$objWMIService is not an object.")
    Exit
EndIf

; Query for sites
$colItems = $objWMIService.ExecQuery("Select * from IIsWebServerSetting")
If $colItems.count > 0 Then
; Build array of sites
    ReDim $avArray[$colItems.count + 1][3]
    $avArray[0][0] = $colItems.count
    $n = 1
    For $oItem In $colItems
        $avArray[$n][0] = $oItem.ServerComment
        $avArray[$n][1] = $oItem.LogFileDirectory
        $avArray[$n][2] = $oItem.Name
        $n += 1
    Next

; Build GUI
    $hGUI = GUICreate("Combo Test", 400, 200)
    $ctrlCombo = GUICtrlCreateCombo($avArray[1][0], 20, 20, 360, 100)
    $ctrlLabel_1 = GUICtrlCreateLabel("Selected Name: ", 20, 60, 360, 20)
    $ctrlLabel_2 = GUICtrlCreateLabel("Selected Path: ", 20, 100, 360, 20)
    $ctrlButton = GUICtrlCreateButton("READ", 150, 150, 100, 30)
    If $avArray[0][0] > 1 Then
        For $n = 2 To $avArray[0][0]
            GUICtrlSetData($ctrlCombo, $avArray[$n][0])
        Next
    EndIf
    GUISetState()
    
; GUI message loop
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $ctrlButton
            ; Read combo and set labels
                $sData = GUICtrlRead($ctrlCombo)
                GUICtrlSetData($ctrlLabel_1, "Selected Name: " & $sData)
                For $n = 1 To $avArray[0][0]
                ; Find matching entry in array
                    If $avArray[$n][0] = $sData Then
                        GUICtrlSetData($ctrlLabel_2, "Selected Path: " & $avArray[$n][1] & "\" & StringReplace($avArray[$n][2],"/","") & "\")
                        ExitLoop
                    EndIf
                Next
        EndSwitch
    WEnd
Else
    MsgBox(16, "Error", "Collection contained no objects.")
    Exit
EndIf
Link to comment
Share on other sites

Awesome, currenly trying to learn what each procedure does but already found out i needed another var "$oItem.Name" to get to the fll path

Now yer lern'n! And there's only one ReDim in the whole thing!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...