Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Skipping array entries

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,38366
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Skipping array entries
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Skipping array entries - 10/4/2006 10:24:47 PM   
  tkdvipers

 

Posts: 7
Score: 0
Joined: 9/29/2006
Status: offline
Good morning everyone.

I have been searching for an hour now and its got to the point where my head is to fuzzy to even understand my very basic code.

I have a for next loop moving through an array, what I want to do is make the loop ignore the values 1, 2, 3, 4 and all.

Is there a way of making the for next loop move to the next entry of the array if it encounters any of these values?

I was going to use select case to identify the chracters but am not sure how to tell the script to move onto the next entry if it does come
across any of the above.

Thanks in advance
 
 
Post #: 1
 
 RE: Skipping array entries - 10/4/2006 10:32:42 PM   
  Snipah


Posts: 1343
Score: 6
Joined: 11/1/2004
From: Netherlands
Status: offline
User the STEP for it

quote:

 
Step Keyword
Using the Step keyword, you can increase or decrease the counter variable by the value you specify.
In the example below, the counter variable (i) is increased by two each time the loop repeats.




For i=2 To 10 Step 2
some code
Next
To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value.
In the example below, the counter variable (i) is decreased by two each time the loop repeats.




For i=10 To 2 Step -2
some code
Next


_____________________________

For more information, please see the "Read me First" topic.

http://www.visualbasicscript.com

(in reply to tkdvipers)
 
 
Post #: 2
 
 RE: Skipping array entries - 10/5/2006 2:55:12 AM   
  tkdvipers

 

Posts: 7
Score: 0
Joined: 9/29/2006
Status: offline
Thankyou   Snipah

I have managed to get my script to do what I want it to do using your advice. However once again I do not understand why it works. I am going to post my script up here and if anybody can explain why the  "killswitch" sub works then I would be much obliged.

Option Explicit

Dim strUsrInput, boofinish, objShell, arrChoices, arrChEntry, strRestart, counter, booanswer

Set objShell = WScript.CreateObject("Wscript.Shell")

Input

checkentry



Function Input
'The following loop, asks the user to input either a computer name or a preser option,
'this will then be passed to the shutdown function. The preset options are prewritten
'batch files which are called to take over the shutdown process
Do
  strUsrInput  = InputBox("Please enter the computer name you wish to shutdown. " & vbCrLf & _
          "If you wish to shutdown more than one computer please " & vbCrLf & _
          "seperate the computer names using a comma but no space, for example: " & vbCrLf & _
          VbCrLf & _
          "computername,computername,computername" & vbCrLf & _
          VbCrLf & _
          "The following options are also avalible: " & vbCrLf & _
          "1 - Will shutdown room 1 " & vbCrLf & _
          "2 - Will shutdown room 2 " & vbCrLf & _
          "3 - Will shurdown room 3 " & vbCrLf & _
          "4 - Will shutdown room 4 " & vbCrLf & _
          "ALL - Will shutdown all machines on the curriculam network except the servers " & vbCrLf & _
          vbCrLf & _
          vbCrLf & _
          "Press cancel to end this script")
         
          If strUsrInput = "" Then ' If the user does not make an entry the script will close
          strRestart = MsgBox("You did not make an entry, press Yes to go back to the beginnig, or No to finish", 4)
              If strRestart = vbYes Then
              Input
              Else
              WScript.Quit
          End If
          End If
         
          arrChoices = Split(strUsrInput,",")'puts the choices the user has made into an Array
          boofinish = MsgBox("If you have finished press Yes, if you would like to change your entries press No", 4)           

Loop Until boofinish = vbYes
End Function

Sub checkentry
For Each arrChEntry In arrChoices
Select Case arrChEntry
Case "1"    MsgBox "this has worked"
Case "2"    objShell.Run "c:\test.bat"
Case "3"    objShell.Run "c:\test.bat"
Case "4"    objShell.Run "c:\test.bat"
Case "ALL"    objShell.Run "c:\test.bat"
Case Else
KillSwitch
End Select
Next
End Sub

Sub KillSwitch
objShell.Run "CMD"
WScript.Sleep "500"
  For counter = 0 To UBound(arrChoices) Step 1
      Select Case arrChEntry
      Case "1" counter = counter + 1
      Case "2" counter = counter + 1
      Case "3" counter = counter + 1
      Case "4" counter = counter + 1
      Case "ALL" counter = counter + 1
      End select
      Next
      objShell.SendKeys "shutdown /r /m \\" & arrChEntry & "~"   
End Sub


This script is not finished I still have to tweak. But it is nearly there. In the above I have set the variable counter to increase by 1 every loop. However if it comes across an entry in the array it adds 1 to the counter before the next statement. This has stopped the numbers 1,2,3,4 and the word "all" being recognised which is what I want. However why are they not recognised, is it because the counter is being added too? Is the loop exiting because the counter is is not equal to itself when the loop begins again?

I have read some books and this is my first script however I am still confused. Thanks for anyhelp that can be provided.

< Message edited by tkdvipers -- 10/5/2006 7:12:57 PM >

(in reply to Snipah)
 
 
Post #: 3
 
 RE: Skipping array entries - 10/5/2006 6:44:29 AM   
  mbouchard


Posts: 1924
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
First, a personal request, do not use the green text.  Oh it hurts.  With that out of the way...

In relation to KillSwitch.  Doing This

 For counter = 0 To UBound(arrChoices) Step 1

Is the same as doing

For counter = 0 To UBound(arrChoices)

From the Help docs
step - Amount counter is changed each time through the loop. If not specified, step defaults to one. Here are 2 examples
With Step 1

      
Without Step 1

      

Now, looking at your Code.  It seems that you are 1) steping using the For next and then in your select advandcing the counter using counter = counter + 1.  I am wondering why you were doing that?

What exactly is it that you are trying to do?  the reason I ask is while I have an idea, it looks a little convoluted to me.

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to tkdvipers)
 
 
Post #: 4
 
 RE: Skipping array entries - 10/5/2006 7:11:22 PM   
  tkdvipers

 

Posts: 7
Score: 0
Joined: 9/29/2006
Status: offline
First off sorry about the green text. It looked red to me (colour-blind).

The script probably is very convoluted as it was my first proper script which I designed and came up with on my own. The books and videos are ok however I found that by making my own scripts it concreted the various objects, methods and properties in my mind.

The killswitch sub is there to deal with any computer names which are entered into the inputbox by the user. The user can enter numbers 1 - 4 and the word All, this corresponds to IT suites at my place of work and "all" is obviously every computer in the building.

The sub "checkentry" checks to see if the user entered a number between 1-4 or the word "all" and runs a premade batch file. Anything else would be sent to a killswitch and a command prompt using send keys.

The problem was that if the user entered for example "1,2,Vanilla" the killswitch sub would try and shutdown a computer named 1and then 2 which it couldn't as there isn't one. I wanted the killswitch sub to skip over an array entry if it had the value of 1 - 4 or all and just move onto the next entry.

It does do this now and I agree it most defiantly looks convoluted, but I want to know why it works.

I can see that in killswitch I am incrementing the counter to a value of "1" before it reaches next which would increment it by one. This seems to stop the numbers 1-4 and the word  "all" being recognised, but there is not point doing this if I can't learn why it’s ignoring those values. Having a select case in between seemed the right thing to do, I basically was saying.

If arrChEntry has the value of 1, 2, 3, 4 or all then move onto the next entry.

Thanks in advance 



JACKSON

(in reply to mbouchard)
 
 
Post #: 5
 
 RE: Skipping array entries - 10/5/2006 11:55:10 PM   
  mbouchard


Posts: 1924
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
quote:

ORIGINAL: tkdvipers

First off sorry about the green text. It looked red to me (colour-blind).

The script probably is very convoluted as it was my first proper script which I designed and came up with on my own. The books and videos are ok however I found that by making my own scripts it concreted the various objects, methods and properties in my mind.

The killswitch sub is there to deal with any computer names which are entered into the inputbox by the user. The user can enter numbers 1 - 4 and the word All, this corresponds to IT suites at my place of work and "all" is obviously every computer in the building.

The sub "checkentry" checks to see if the user entered a number between 1-4 or the word "all" and runs a premade batch file. Anything else would be sent to a killswitch and a command prompt using send keys.

The problem was that if the user entered for example "1,2,Vanilla" the killswitch sub would try and shutdown a computer named 1and then 2 which it couldn't as there isn't one. I wanted the killswitch sub to skip over an array entry if it had the value of 1 - 4 or all and just move onto the next entry.

It does do this now and I agree it most defiantly looks convoluted, but I want to know why it works.

I can see that in killswitch I am incrementing the counter to a value of "1" before it reaches next which would increment it by one. This seems to stop the numbers 1-4 and the word  "all" being recognised, but there is not point doing this if I can't learn why it's ignoring those values. Having a select case in between seemed the right thing to do, I basically was saying.

If arrChEntry has the value of 1, 2, 3, 4 or all then move onto the next entry.

Thanks in advance 



JACKSON

Don't worry about the text.  Also, do not worry about the script looking convoluted, believe me, if you look at my first scripts they will look just as bad if not worse.

Looking at the original code, killswitch is not being called if 1,2,3,4 or all is selected.  Only with Case Else.  Basically, having the 2 Select Case's in the 2 subs is not needed, as the first sub calls a bat file if the case is met.

Incrementing the counter doesn't do anything for calling the shutdown command, as everything happens in the for next once it is out of there it still shutdowns what ever is chosen in the ArrChEntry.

For counter = 0 To UBound(arrChoices) Step 1
      Select Case arrChEntry
      Case "1" counter = counter + 1
      Case "2" counter = counter + 1
      Case "3" counter = counter + 1
      Case "4" counter = counter + 1
      Case "ALL" counter = counter + 1
      End select
      Next
      objShell.SendKeys "shutdown /r /m \\" & arrChEntry & "~"   

You could redo your killswitch sub to something like this

      

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to tkdvipers)
 
 
Post #: 6
 
 RE: Skipping array entries - 10/6/2006 1:27:46 AM   
  tkdvipers

 

Posts: 7
Score: 0
Joined: 9/29/2006
Status: offline
I can not believe it. Thank you so much for helping me with that. I was so focused on getting a certain task done that I didn't even look at the logic.

What a dunce lol.

I revised my code, and just put the code I needed to run into the checkEntry sub. I wrote so much psuedo code trying to figure this out and I have should have just taken a step back and not tried to be so complicated.

Option Explicit

Dim strUsrInput, boofinish, objShell, arrChoices, arrChEntry, strRestart, counter, booanswer

Set objShell = WScript.CreateObject("Wscript.Shell")

Input

checkentry

Function Input ' Main input Function
'The following loop, asks the user to input either a computer name or a preser option,
'this will then be passed to the shutdown function. The preset options are prewritten
'batch files which are called to take over the shutdown process
Do
   strUsrInput  = Lcase(InputBox("Please enter the computer name you wish to shutdown. " & vbCrLf & _  
           "If you wish to shutdown more than one computer please " & vbCrLf & _
           "seperate the computer names using a comma but no space, for example: " & vbCrLf & _
           VbCrLf & _
           "computername,computername,computername" & vbCrLf & _
           VbCrLf & _
           "The following options are also avalible: " & vbCrLf & _
           "1 - Will shutdown room 1 " & vbCrLf & _
           "2 - Will shutdown room 2 " & vbCrLf & _
           "3 - Will shurdown room 3 " & VbCrLf & _
           "4 - Will shutdown room 4 " & vbCrLf & _
           "ALL - Will shutdown all machines on the curriculam network except the servers " & vbCrLf & _
           vbCrLf & _
           vbCrLf & _
           "Press cancel to end this script"))
          
           If strUsrInput = "" Then ' If the user does not make an entry the script will close
           strRestart = MsgBox("You did not make an entry, press Yes to go back to the beginnig, or No to finish", 4)
               If strRestart = vbYes Then
               Input
               Else
               WScript.Quit
           End If
           End If
          
           arrChoices = Split(strUsrInput,",")'puts the choices the user has made into an Array
           boofinish = MsgBox("If you have finished press Yes, if you would like to change your entries press No", 4)           

Loop Until boofinish = vbYes
End Function

Sub checkentry ' checks to see if the user entered any key words
For Each arrChEntry In arrChoices
Select Case arrChEntry
Case "1"    objShell.Run "n:\shutdown\room1.bat"
Case "2"    objShell.Run "n:\shutdown\room2.bat"
Case "3"    objShell.Run "n:\shutdown\room3.bat"
Case "4"    objShell.Run "n:\shutdown\room4.bat"
Case "all"    objShell.Run "n:\shutdown\all.bat"
Case Else
objShell.Run "CMD"
WScript.Sleep "500"
objShell.SendKeys "shutdown /r /m \\" & arrChEntry & "~"
End Select
Next
End Sub


This is the end product. Exactly the same minus one sub. I won't forget what I learnt here.

Jackson

(in reply to mbouchard)
 
 
Post #: 7
 
 RE: Skipping array entries - 10/8/2006 11:03:54 PM   
  mbouchard


Posts: 1924
Score: 16
Joined: 5/15/2003
From: USA
Status: offline
Glad to help.

_____________________________

Mike

For useful Scripting links see the Read Me First stickey!

Always remember Search is your friend.

(in reply to tkdvipers)
 
 
Post #: 8
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Skipping array entries Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts