Sign in to follow this  
Followers 0
Bert

Array items

6 posts in this topic

How does one get the count of array items?

Example:

IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "" )
 $hService = ObjGet ("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")
    $sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
     For $sPrinter In $sPrinterList 
  guictrlsetdata($_dummy, $sPrinter.name)
  $rd3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")
  IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", $rd3 & $sPrinter.name & "|" )
 next
 $local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|")

When I run this, I get a return of 7 items. If I run this on another PC, I will get a return of 3 items, for they only have 3 printers installed. How do I know how many items there are?

This is a example script you can use to test with. You can remark out the number of items set to match the number of printers to see how it works on your PC. Any help would be good.

CODE
#include <GuiConstants.au3>

#include <Process.au3>

#include <Array.au3>

GuiCreate("MyGUI", 241, 347,-1, -1 , BitOR($WS_OVERLAPPEDWINDOW, $WS_CLIPSIBLINGS))

$_dummy = GUICtrlCreateDummy()

$Input_1 = GuiCtrlCreateInput("Input1", 30, 30, 180, 20)

$Input_2 = GuiCtrlCreateInput("Input2", 30, 60, 180, 20)

$Input_3 = GuiCtrlCreateInput("Input3", 30, 90, 180, 20)

$Input_4 = GuiCtrlCreateInput("Input4", 30, 120, 180, 20)

$Input_5 = GuiCtrlCreateInput("Input5", 30, 150, 180, 20)

$Input_6 = GuiCtrlCreateInput("Input6", 30, 180, 180, 20)

$Input_7 = GuiCtrlCreateInput("Input7", 30, 210, 180, 20)

$Input_8 = GuiCtrlCreateInput("Input8", 30, 240, 180, 20)

$Input_9 = GuiCtrlCreateInput("Input9", 30, 270, 180, 20)

$Input_10 = GuiCtrlCreateInput("Input10", 30, 300, 180, 20)

_getinstalledPrinters()

GuiSetState()

While 1

$msg = GuiGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case Else

;;;

EndSelect

WEnd

Exit

func _getinstalledPrinters()

IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "" )

$hService = ObjGet ("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")

$sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")

For $sPrinter In $sPrinterList

guictrlsetdata($_dummy, $sPrinter.name)

$rd3 = IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", "")

IniWrite(@ScriptDir & "/npu.ini", "Printers", "LOCAL", $rd3 & $sPrinter.name & "|" )

next

$local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|")

$list1 = guictrlsetdata($input_1, $local[1])

$list2 = guictrlsetdata($input_2, $local[2])

$list3 = guictrlsetdata($input_3, $local[3])

$list4 = guictrlsetdata($input_4, $local[4])

$list5 = guictrlsetdata($input_5, $local[5])

$list6 = guictrlsetdata($input_6, $local[6])

$list7 = guictrlsetdata($input_7, $local[7])

$list8 = guictrlsetdata($input_8, $local[8])

$list9 = guictrlsetdata($input_9, $local[9])

$list10 = guictrlsetdata($input_10, $local[10])

EndFunc

Share this post


Link to post
Share on other sites



$hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")
$sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
ConsoleWrite('$sPrinterList.count = ' & $sPrinterList.count & @lf)
For $sPrinter In $sPrinterList
    ConsoleWrite('$sPrinter.name = ' & $sPrinter.name & @LF)
Next


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Jon,

Thanks for the quick responce, but could you explain how I'm to use this? I tried it, and I still get the same error. I see how it gets the count in the console, but how do I then use it to set the data in the fields correctly, and get past the error? I have to use this on a lot of PCs, so I don't know what printers they have installed.

func _getinstalledPrinters()
$hService = ObjGet("winmgmts:{impersonationLevel=impersonate}!" & "\\" & @ComputerName & "\root\cimv2")
$sPrinterList = $hService.ExecQuery ("Select * From Win32_Printer")
ConsoleWrite('$sPrinterList.count = ' & $sPrinterList.count & @lf)
For $sPrinter In $sPrinterList
    ConsoleWrite('$sPrinter.name = ' & $sPrinter.name & @LF)
Next 
 $local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|")
 $list1 = guictrlsetdata($input_1, $local[1]) 
 $list2 = guictrlsetdata($input_2, $local[2]) 
 $list3 = guictrlsetdata($input_3, $local[3])
 $list4 = guictrlsetdata($input_4, $local[4])
 $list5 = guictrlsetdata($input_5, $local[5])
 $list6 = guictrlsetdata($input_6, $local[6])
 $list7 = guictrlsetdata($input_7, $local[7])
 $list8 = guictrlsetdata($input_8, $local[8])
 $list9 = guictrlsetdata($input_9, $local[9])
 $list10 = guictrlsetdata($input_10, $local[10])

EndFunc
Edited by vollyman

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Jon,

Nah... close, only 1 wrong character, but totally different person... :D

Thanks for the quick responce, but could you explain how I'm to use this? I tried it, and I still get the same error. I see how it gets the count in the console, but how do I then use it to set the data in the fields correctly, and get past the error? I have to use this on a lot of PCs, so I don't know what printers they have installed.

You could just redim the array to ensure it alwas contains enough entries:

$local = StringSplit(IniRead(@ScriptDir & "/npu.ini", "Printers", "LOCAL", ""), "|")
 If UBound($local) < 11 Then ReDim $local[11]
 $list1 = guictrlsetdata($input_1, $local[1]) 
 $list2 = guictrlsetdata($input_2, $local[2]) 
 $list3 = guictrlsetdata($input_3, $local[3])
 $list4 = guictrlsetdata($input_4, $local[4])
 $list5 = guictrlsetdata($input_5, $local[5])
 $list6 = guictrlsetdata($input_6, $local[6])
 $list7 = guictrlsetdata($input_7, $local[7])
 $list8 = guictrlsetdata($input_8, $local[8])
 $list9 = guictrlsetdata($input_9, $local[9])
 $list10 = guictrlsetdata($input_10, $local[10])
Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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