Link to home
Start Free TrialLog in
Avatar of Duncan Driggers
Duncan Driggers

asked on

Can VBS count the number of items in an array

Trying to write a versatile script that within a Select case the number of items can be counted from an array in a separate file.
My current body looks like this

Set args = WScript.Arguments
WScript.Echo vbCrLf

Set fos = CreateObject("Scripting.FileSystemObject")
Set ipad = fso.OpenTextFile("C:\x\x\x\x",1,false)
IPadd = ipad.ReadAll
Set ipad=nothing
Set fso=nothing
ExexuteGlobal IPadd

Select Case args.Item (0)
	Case "1"
		Call Individual_Router 'Allows the user to ping one router
	Case "2"
		Call All_Routers'Or all routers
	Case else
		WScript.Echo chr(7)  & "Error, Choices are All routers, or Individual routers" 'In the event that no adiquate router is chosen error message will appear
		
	Sub Individual_Router
		Select Case args.item ()

The array looks like this

Dim ipaddress()
ipaddress= "8.8.8.8"
ipaddress= "8.8.4.4"
ipaddress= "74.125.224.72"
ipaddress= "69.63.176.13"
ipaddress= "0.0.0.0"

Open in new window

However if the array were to have additional or fewer ip addresses added to it, i would like to not be forced to change the script that i am running, and just change the array file.
Avatar of Shaun Vermaak
Shaun Vermaak
Flag of Australia image

UBound(ipaddress) + 1

Open in new window

The exact formula is UBound(ipaddress)-LBound(ipaddress)+1, but since an array index starts with 0 by default, Shaun's formula works most of the time.
Avatar of Bill Prew
Bill Prew

Can you show the array based logic you are using today, that would help with feedback?  Are you using Split() to populate it, or some other method?

Keep in mind that you can use FOR EACH over an array, and process each element of it without regard to the number of elements.  So the real issue is often with the dimensioning of the array, which is why I'd like to see more of the code...

~bp
Avatar of Duncan Driggers

ASKER

I am fairly new to scripting, so excuse my confusion.
In refrence to the following quote, if by populate you mean how am i filling the array, that would all be user input.
Are you using Split() to populate it, or some other method?

My goal with this is to have the user fill out a number of IPs that they would like to check for signs of life on the IPadd(ress) file then have them run the main file. The main file will ping the number of ips listed (could be any number of IPs that is my hope anyway), possibly have a new file created that writes down all of the ping information, and then returns to the user, and based on the select case argument selected, state that the specific chosen router is or isnt working  OR the status of all routers. I have an idea of how to do this, but i dont wish to write my self into a corner with too many problems to solve so i am trying to solve the issues as i get to them.
Since you are populating the array one by one with user input, I would let the array grow dynamically as each IP is entered by the user.  Start with just one entry in it, and then add another every time they enter a new IP.  There is a command in VBS that allows redimensioning an array, called REDIM.  So the basic idea would be:


' Inside the prompt for IPs loop...
maxIP = maxIP + 1
ReDim Preserve arrIP(maxIP)
arrIP(MaxIP) = newIP

Open in new window

Bill, as I read it the OP wants to collect IPs in a file first. But IMHO there is no reason to ask for IPs in the code directly ...
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As im new to scripting, im unsure exactly what im looking for, though im trying to make it as clear as i can.
in this bit of code
		Sub Individual_Router
		     Select Case args.item ()

		End Sub

Open in new window

I am attempting to call upon the array on the other file, to populate the choices for the selection. So that if the IPs in the second file are changed that i wont have to come back into the main file and change anything to it, and still be able to populate, more or fewer IPs as the case may be.
I have been looking around for different ways to do this, and as i havent used every type of function and code im not sure exactly how to use the suggestion that Bill gave me, I do appreciate the assistance though, just thought i would try and clear it up
EDIT
After taking a minute to look
using the code bill suggested,
		
	Sub Individual_Router
		Select Case args.item ()
		For Each strIP in Split(IPadd, vbCrLf)
			If strIP <> "" Then
				Case "1"
				
		End If
		End Sub

Open in new window

How could i then have it count the cases per provided IP?
so if for each IP found, i want to to have a select case for each one.
so in the CMD window you would see the list of ips as the selection?

Edit
For the sake of trouble shooting the whole file as i try and make advances, for the All routers module i have written
		
	Sub All_Routers
		For Each strIP in Split(IPadd, vbCrLf)
			If strIP <> "" Then
			SET objPing = GETOBJECT ("winmgmts:{impersinationLevel=impersonate}")
				ExecQuery("select * from Win32_PingStatus where address= '" & strIP & "'")
			For Each objPingStatus in objPing
				IF IsNull (objPingStatus.StatusCode) OR objPingStatus.StatusCode<>0 Then
					If strFaildPings<> "" Then str FailedPings = strFailedPings & vbCrLf
					strFailedPings = strFailedPings & strIP
				End IF
			Next

Open in new window


any comments are appreciated.