Sign in to follow this  
Followers 0
Bert

Array question

16 posts in this topic

For some reason, when I deal with Arrays, it is like kryptonite to Superman. I'm working on a script to install Windows network printers. What I'm attempting is to capture the information from a GuiCtrlCreateCombo and put it in a ini file so I can retrieve it later when needed. My attempt to do this failed miserably. HELP!

#include <GuiConstants.au3>
DIM $hService, $sPrinter, $sPrinterList, $data
GuiCreate("", 205, 290,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GuiCtrlCreateLabel("//", 20, 52, 10, 20)
$iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "")
$iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
$Input_1 = GuiCtrlCreateInput($iniread_1, 35, 50, 100, 20)
$Combo_3 = GuiCtrlCreateCombo($iniread_2, 20, 75, 170, 21)
$Combo_4 = GuiCtrlCreateCombo($iniread_2, 20, 100, 170, 21)
$Combo_5 = GuiCtrlCreateCombo($iniread_2, 20, 125, 170, 21)
$Combo_6 = GuiCtrlCreateCombo($iniread_2, 20, 150, 170, 21)
$Combo_7 = GuiCtrlCreateCombo($iniread_2, 20, 175, 170, 21)
$Combo_2 = GuiCtrlCreateCombo($iniread_2, 20, 200, 170, 21)
$Button_11 = GuiCtrlCreateButton("Run", 20, 225, 80, 30)
$Button_12 = GuiCtrlCreateButton("Exit", 117, 225, 73, 30)
$button_14a = GuiCtrlCreateButton("Find", 146, 50, 43, 20)
$Label_15 = GuiCtrlCreateLabel("   Network Printer Utility" &  @CRLF & "for Windows Print Servers", 20, 0, 340, 50)
$font="Comic Sans MS"
GUICtrlSetFont($Label_15, 10, 400, "", $font)
$Label_16 = GuiCtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25)

GuiSetState()
While 1
 $msg = GuiGetMsg()
 Select
 Case $msg = $GUI_EVENT_CLOSE
  ExitLoop
    Case $msg = $Button_11
  MsgBox(0, "", "run button pressed")
 Case $msg = $button_14a
  $data = GUICtrlRead($Input_1, 0)
  ;MsgBox(0, "", $data)
  MsgBox(0, "", "This may take a moment...Please wait until the search for printer share is complete.", 2)
  _netprint1()
 Case $msg = $Button_12
  Exit 
 Case Else
  ;;;
 EndSelect
WEnd
Exit

func _netprint1()
 ;Local $hService, $sPrinter, $sPrinterList
    ;$hService = ObjGet ("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")
 $hService = ObjGet ("winmgmts:{impersonationLevel=impersonate}!" & "\\" & $data & "\root\cimv2")
    If Not @error = 0 Then
        MsgBox(48, "ERROR", "No Printers Found. Possible issues: " & @CRLF _  
       & ""& @CRLF _
       & "  1. The Windows Print Server name has been entered in incorrectly."& @CRLF _
       & "  2. You are trying to access a Novell Server. This utility does not support Novell Print Servers."& @CRLF _
       & "  3. There are no printers shared on the Windows Print Server you selected.")
 Else 
  _NP2() 
    EndIf
EndFunc
func _np2()
 ;Local $hService, $sPrinter, $sPrinterList
    $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
 guictrlsetdata($combo_2, "")
 guictrlsetdata($combo_3, "")
    guictrlsetdata($combo_4, "")
 guictrlsetdata($combo_5, "") 
    guictrlsetdata($combo_6, "")
 guictrlsetdata($combo_7, "")
    For $sPrinter In $sPrinterList
  guictrlsetdata($combo_2, $sPrinter.name)
  guictrlsetdata($combo_3, $sPrinter.name)
  guictrlsetdata($combo_4, $sPrinter.name)
  guictrlsetdata($combo_5, $sPrinter.name) 
  guictrlsetdata($combo_6, $sPrinter.name)
  guictrlsetdata($combo_7, $sPrinter.name)
 Next
    $dataget1 = GUICtrlRead($combo_2, 0)
 MsgBox(0, "", $dataget1)
 IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $dataget1)
 $dataget2 = GUICtrlRead($Input_1, 0)
 IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2)
MsgBox(0, "Done!", "Select what printers you wish to install")
endfunc

Share this post


Link to post
Share on other sites



Well, after looking it over, one potential problem I see is that you don't set a default value for the combo boxes. So then when you use GUICtrlSetData right before attempting a GUICtrlRead, the boxes aren't going to be displaying a value, which means that GUICtrlRead is going to return nothing (GUICtrlRead only reads the displayed value in the combo box). Therefore, there's nothing to write to the .ini file. The way the data is set, it is only added to the available choices in the drop-down menu, but that's also not until you're trying to write the values to the .ini file.

Here is a suggestion, but I'm not sure if this is what you are after, change this:

guictrlsetdata($combo_2, $sPrinter.name)oÝ÷ ÚÚ-+ºÚ"µÍÝZXÝÙ]]J    ÌÍØÛÛX×Ì ÌÍÜÔ[[YK    ÌÍÜÔ[[YJ

This will set that value as a choice in the menu, and at the same time, display it as the default selection in the combo box which will allow it to be read by GUICtrlRead.

Nomad :D


Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I need the entire list that will be gathered when the find button is pressed after you enter in the server name. When I run it, I will collect over 200 windows printers that is listed in each combo box. I need to save this to the ini, so I don't need to run the find function unless I need to refresh the list. The Find function takes a while to run, so I wanted to speed it up by having the information cashed. (I.e. installing on many PCs) Also, I want the fields to be blank at first so I will only install the printers I need, not install 6 no matter what.

Edited by vollyman

Share this post


Link to post
Share on other sites

I need the entire list that will be gathered when the find button is pressed after you enter in the server name. When I run it, I will collect over 200 windows printers that is listed in each combo box. I need to save this to the ini, so I don't need to run the find function unless I need to refresh the list. The Find function takes a while to run, so I wanted to speed it up by having the information cashed. (I.e. installing on many PCs) Also, I want the fields to be blank at first so I will only install the printers I need, not install 6 no matter what.

Ah, I see. I misunderstood what is was you were after. I'll have to think on that, hehe. :D

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I need the entire list that will be gathered when the find button is pressed after you enter in the server name. When I run it, I will collect over 200 windows printers that is listed in each combo box. I need to save this to the ini, so I don't need to run the find function unless I need to refresh the list. The Find function takes a while to run, so I wanted to speed it up by having the information cashed. (I.e. installing on many PCs) Also, I want the fields to be blank at first so I will only install the printers I need, not install 6 no matter what.

I've been thinking about it, and this is the best option I have come up with so far:

Possibly writing all the names that are going to be entered in the combo box into a text file, then reading the names from the text file into a multi-dimensional array. Then sorting the multi-dimensional array into the respective combo boxes while reading the names into the .ini file all done inside a For/Next loop. The array would have to be multi-dimensional since you have more than 64 names to store. Maybe even possibly more than 1 array. I've never dealt with arrays this size, so I'm not sure of it's limits.

Have you considered something along this line?

Edited by Nomad

Share this post


Link to post
Share on other sites

I was trying to write it to a ini file, and that is the problem. I don't know how to do it. I can get only one printer, and I need the entire list. I know I need to do it with a array, but I do not know how to do it. Like I said at the beginning of the thread, when it comes to designing a array, it is like kryptonite to Superman in my head.

I was thinking of writing the string like this: printer1|printer2|printer3|printer4|.... and so forth. What is stumping me is how to get it into that format.

Maybe

local $file1 = fileopen(@ScriptDir & "info.txt", 1)
For $sPrinter In $sPrinterList
  guictrlsetdata($combo_2, $sPrinter.name)
  filewrite($file1, $sPrinter.name & "|")
next

Not sure this will work, for I can't test it until next monday. What do you think?

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Is $sPrinterList already an array? If so you can do something like this:

$ArraySize = UBound($sPrinterList) - 1

For $i = 0 to $ArraySize
 $PrinterNumber = "Printer" & ($i + 1)
 IniWrite(@ScriptDir & "/npu.ini", "Printers", $PrinterNumber, $sPrinterList[$i])
Next

If it's not in an array and you are trying to figure out how to make it an array, what does $hService.ExecQuery return? Is it a single printer, or the entire list of printers?

If it's the complete list, then $sPrinterList should already be an array and the example above should work, unless the array is multi-dimensional. Then I would need to make a new example, but it's still not hard. I would need more information on how the array is structured though, like what are the min and max indices for the printer name values in the last dimension ($Array[$i][min-max])

If $hService.ExecQuery returns a single printer, then how are you determining which printer name is being returned? I can make it write to an array if I know how you are determining the printer name returned.

Nomad :D

Edited by Nomad

Share this post


Link to post
Share on other sites

Nomad, it is a collection.

vollyman, try to test it in a test script.

This shows one printer, but I only have 1 available

$data = @ComputerName
$oWMIService = ObjGet ("winmgmts:\\" & $data & "\root\cimv2")
$oCollection = $oWMIService.ExecQuery("SELECT * FROM Win32_Printer")
If IsObj($oCollection) Then
    For $oItem In $oCollection
        MsgBox(0, '', $oItem.Name)
    Next
EndIf

If your printers are on a share, then perhaps Win32_PrinterShare would give you the full list of printers?

Share this post


Link to post
Share on other sites

Nomad, it is a collection.

Oh, ok. I've never messed with anything like this. I know I could help him if I know how the data is being collected and stored. Unfortunately I don't know since I only have a single printer as well. But since nobody else was offering suggestions I thought I'd give it a shot. :D

Share this post


Link to post
Share on other sites

There is a way to check, and I thought of this while working out in the yard this morning. If you have a network, simply make some printers on a PC and share them. THey don't need to be hooked up to a real one, just the drivers need to be installed so windows reports them. All I need to make is like 3 or 4, and test. That would tell me if it would work. Also, I thought why write the information to a txt file, then to a ini file. I could just write it to a variable, then to a ini. I don't think the data is written in a array in the first place. When I first tested to see how it was delivered, I ran the script like this:

[/size] For $sPrinter In $sPrinterList
  guictrlsetdata($combo_2, $sPrinter.name)
  msgbox(0, "",$sPrinter.name)
next

It would post one printer in the msgbox. When you closed the msgbox, it would pop back up with the next printer it found.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

There is a way to check, and I thought of this while working out in the yard this morning. If you have a network, simply make some printers on a PC and share them. THey don't need to be hooked up to a real one, just the drivers need to be installed so windows reports them. All I need to make is like 3 or 4, and test. That would tell me if it would work. Also, I thought why write the information to a txt file, then to a ini file. I could just write it to a variable, then to a ini. I don't think the data is written in a array in the first place. When I first tested to see how it was delivered, I ran the script like this:

[/size] For $sPrinter In $sPrinterList
  guictrlsetdata($combo_2, $sPrinter.name)
  msgbox(0, "",$sPrinter.name)
next

It would post one printer in the msgbox. When you closed the msgbox, it would pop back up with the next printer it found.

Based on that, I created this:
For $sPrinter In $sPrinterList
 GUICtrlSetData($combo_2, $sPrinter.name)
 $i += 1
 $PrinterNumber = "Printer" & $i)
 $PrinterName = $sPrinter.name
 IniWrite(@ScriptDir & "/npu.ini", "Printers", $PrinterNumber, $PrinterName)
NextoÝ÷ Øh¯zØZµ«kÉIåzË2¢éÞyÖ¥íêk{^®È§jv«­¬­êÞßÛ'¢wvÚ&jGªëk(î²Ûh¸­zÚ-è§ø¥x·­é'£hÂØb³
+Çè¯*.jwb*.çj|­)àr§çM¢fnX§Gb´ò¢çhÁ©íjv«­¬­ Øb²ÈhºW­æ­y©Ú®¶²jÌV¬Â¸­zÚ-è§ø¥y«­¢+Ù½ÈÀÌØíÍAÉ¥¹ÑÈ%¸ÀÌØíÍAÉ¥¹ÑÉ1¥ÍÐ(%U%
ÑɱMÑÑ ÀÌØí½µ½|È°ÀÌØíÍAÉ¥¹Ñȹ¹µ¤($ÀÌØí¤¬ôÄ($ÀÌØíAÉ¥¹ÑÉ9ÕµÈôÅÕ½ÐíAÉ¥¹ÑÈÅÕ½ÐìµÀìÀÌØí¤¤($ÀÌØíAÉ¥¹ÑÉ9µôÀÌØíÍAÉ¥¹Ñȹ¹µ(%%¹¥]ɥѡMÉ¥ÁѥȵÀìÅÕ½Ðì½¹ÁÔ¹¥¹¤ÅÕ½Ðì°ÅÕ½ÐíAÉ¥¹ÑÉÌÅÕ½Ðì°ÀÌØíAÉ¥¹ÑÉ9յȰÀÌØíAÉ¥¹ÑÉ9µ¤)9áÐ()¥´ÀÌØíÉÉålÀÌØí¥t(ÀÌØí¤ôÀ()½ÈÀÌØíÍAÉ¥¹ÑÈ%¸ÀÌØíÍAÉ¥¹ÑÉ1¥ÍÐ($ÀÌØíÉÉålÀÌØí¥tôÀÌØíÍAÉ¥¹Ñȹ¹µ($ÀÌØí¤¬ôÄ)9áÐ
Edited by Nomad

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

I'm really grateful for the help. but none of this is solving my problem. :D I simply need to get all the data that is in the combo into a string. Example: The Combo box could show the following listing after I run a query:

Printer1

Printer2

Printer3

Printer4

Printer5

When I run GUICtrlGetData, I can only get the data being displayed. I need to get the entire list, and convert it to the string "printer1|printer2|printer3|printer4|printer5" Is this possible?

I tried everything everyone suggested. Nothing worked. One simple gave me a code of 8435, and the others errored out right from the start. :wacko:

Edited by vollyman

Share this post


Link to post
Share on other sites

For $sPrinter In $sPrinterList
  $String &= $sPrinter.name & "|"
next
If this doesn't work for that, then I'm out of suggestions.

Nomad :D


Share this post


Link to post
Share on other sites

I tried this:

func _np2()
 local $i
 ;Local $hService, $sPrinter, $sPrinterList
    $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
 guictrlsetdata($combo_2, "")
 guictrlsetdata($combo_3, "")
    guictrlsetdata($combo_4, "")
 guictrlsetdata($combo_5, "") 
    guictrlsetdata($combo_6, "")
 guictrlsetdata($combo_7, "")
 IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", "|")
 $rd1 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
    For $sPrinter In $sPrinterList 
  guictrlsetdata($combo_2, $sPrinter.name)
  guictrlsetdata($combo_3, $sPrinter.name)
  guictrlsetdata($combo_4, $sPrinter.name)
  guictrlsetdata($combo_5, $sPrinter.name) 
  guictrlsetdata($combo_6, $sPrinter.name)
  guictrlsetdata($combo_7, $sPrinter.name)
  $rd1 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
  IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $rd1 + $sPrinter.name & "|" )
 Next
    $dataget1 = GUICtrlRead($combo_2, 0)
 ;MsgBox(0, "", $dataget1)
 IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $dataget1)
 $dataget2 = GUICtrlRead($Input_1, 0)
 IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2)
MsgBox(0, "Done!", "Select what printers you wish to install")
endfunc

Its wierd. While I run my query, I was checking the ini to see what was being written. It was posting 4 digit numbers, and they kept changing. When it was done, it was blank. The real wired thing is, when I use a msgbox, it will show the printer name. real odd.

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

GOT IT! I did this:

func _np2()
 local $i
 ;Local $hService, $sPrinter, $sPrinterList
    $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
 guictrlsetdata($combo_2, "")
 guictrlsetdata($combo_3, "")
    guictrlsetdata($combo_4, "")
 guictrlsetdata($combo_5, "") 
    guictrlsetdata($combo_6, "")
 guictrlsetdata($combo_7, "")
 IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
    For $sPrinter In $sPrinterList 
  guictrlsetdata($combo_2, $sPrinter.name)
  guictrlsetdata($combo_3, $sPrinter.name)
  guictrlsetdata($combo_4, $sPrinter.name)
  guictrlsetdata($combo_5, $sPrinter.name) 
  guictrlsetdata($combo_6, $sPrinter.name)
  guictrlsetdata($combo_7, $sPrinter.name)
  $rd1 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
  IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $rd1 & $sPrinter.name & "|" )
 Next
 $dataget2 = GUICtrlRead($Input_1, 0)
 IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2)
MsgBox(0, "Done!", "Select what printers you wish to install")
endfunc
:D

Thanks to everyone who tried to help. This one was a real head scratcher. I still don't know why it was posting a number instead of the name. I tried to do a array to see if that was it, but it errored when I tried that.

Edited by vollyman

Share this post


Link to post
Share on other sites

Just seen that you got it, but I think this may be a little cleaner.

#include <GuiConstants.au3>

Dim $hService

$iniread_1 = IniRead(@ScriptDir & "/npu.ini", "SERVER", "SERVER", "")
$iniread_2 = IniRead(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
$aPrinters = StringSplit($iniread_2, "|")

GUICreate("", 205, 290, -1, -1, BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))
GUICtrlCreateLabel("//", 20, 52, 10, 20)
$Input_1 = GUICtrlCreateInput($iniread_1, 35, 50, 100, 20)
$Combo_3 = GUICtrlCreateCombo("", 20, 75, 170, 21)
$Combo_4 = GUICtrlCreateCombo("", 20, 100, 170, 21)
$Combo_5 = GUICtrlCreateCombo("", 20, 125, 170, 21)
$Combo_6 = GUICtrlCreateCombo("", 20, 150, 170, 21)
$Combo_7 = GUICtrlCreateCombo("", 20, 175, 170, 21)
$Combo_2 = GUICtrlCreateCombo("", 20, 200, 170, 21)
If IsArray($aPrinters) Then
    For $i = 1 To $aPrinters[0] - 1
        For $y = 2 To 7
            GUICtrlSetData(Eval("Combo_" & $y), $aPrinters[$i])
        Next
    Next
EndIf
$Button_11 = GUICtrlCreateButton("Run", 20, 225, 80, 30)
$Button_12 = GUICtrlCreateButton("Exit", 117, 225, 73, 30)
$button_14a = GUICtrlCreateButton("Find", 146, 50, 43, 20)
$Label_15 = GUICtrlCreateLabel("   Network Printer Utility" & @CRLF & "for Windows Print Servers", 20, 0, 340, 50)
$font = "Comic Sans MS"
$Label_16 = GUICtrlCreateLabel("2006 CTS Scripting Team", 40, 270, 130, 25)
GUICtrlSetFont($Label_15, 10, 400, "", $font)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_11
            MsgBox(0, "", "run button pressed")
        Case $msg = $button_14a
            MsgBox(0, "", "This may take a moment...Please wait until the search for printer share is complete.", 2)
            $data = GUICtrlRead($Input_1, 0)
            ;MsgBox(0, "", $data)
            _netprint1($data)
        Case $msg = $Button_12
            Exit
        Case Else
            ;;;
    EndSelect
WEnd
Exit

Func _netprint1($v_data)
    Local $hService
    
    $hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & $v_data & "\root\cimv2")
    If @error Then
        MsgBox(48, "ERROR", "No Printers Found. Possible issues: " & @CRLF _
                 & "" & @CRLF _
                 & "  1. The Windows Print Server name has been entered in incorrectly." & @CRLF _
                 & "  2. You are trying to access a Novell Server. This utility does not support Novell Print Servers." & @CRLF _
                 & "  3. There are no printers shared on the Windows Print Server you selected.")
    Else
        _np2()
    EndIf
EndFunc   ;==>_netprint1

Func _np2()
    Local $i = 0
    Local $sPrinters = ""
    Local $aArray[1]
    
    GUICtrlSetData($Combo_2, "")
    GUICtrlSetData($Combo_3, "")
    GUICtrlSetData($Combo_4, "")
    GUICtrlSetData($Combo_5, "")
    GUICtrlSetData($Combo_6, "")
    GUICtrlSetData($Combo_7, "")
    
    $oPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
    IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", "")
    
    For $oPrinter In $oPrinterList
        ReDim $aArray[$i + 1]
        $aArray[$i] = $oPrinter.name
        For $y = 2 To 7
            GUICtrlSetData(Eval("Combo_" & $y), $aArray[$i])
        Next
        $i += 1
    Next
    
    For $x = 0 To UBound($aArray) - 1
        $sPrinters &= $aArray[$x] & "|"
    Next
    
    IniWrite(@ScriptDir & "/npu.ini", "Printers", "PLIST", $sPrinters)
    $dataget2 = GUICtrlRead($Input_1, 0)
    IniWrite(@ScriptDir & "/npu.ini", "SERVER", "SERVER", $dataget2)
    MsgBox(0, "Done!", "Select what printers you wish to install")
EndFunc   ;==>_np2

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