Skip section in vbscript

Microsoft Windows xp professional w/serv...
March 13, 2010 at 06:04:06
Specs: Windows XP
I'm trying to figure out a way to skip a section of a vb script. The reason being is I have the below section which should only be run if a variable = True. If false, I just want it to skip it and continue on.

code...

dn = CheckForCN(empCN)

If dn = "" Then
	WScript.Quit

code...

I was thinking something like this might get passed it but not sure if you can nest IF statements inside other another IF. If there is a cleaner/better way of doing it please let me know.

code...

If connected = "True" Then
    dn = CheckForCN(empCN)
    If dn = "" Then
	WScript.Quit
Else/if connected = "false" Then
Skip section and continue

code...

See More: Skip section in vbscript

Report •


#1
March 13, 2010 at 06:24:28
You don't need to nest If/else. The following modification will work providing the the only values connected can have is True or False. If that is the case you might consider making Connected a boolean type variable then it can only be true or false

If connected = "True" Then
dn = CheckForCN(empCN)
If dn = "" Then
WScript.Quit
End if
End if

Stuart


Report •

#2
March 13, 2010 at 07:38:01
Thanks for your response. Yes, the only choices will be true or false.

So if I script it like you have, what happens if connected = false. Will it just skip this section and continue on?

Also whats the best way of setting the variable "connected" within a case statement of 30+ cases.

I have no scripting skills but was thinking the following. I'm sure there is a more efficient way of doing it though, even if I have to have a case range eg. 1-15 true, 16-30 false.. if you get what I mean.

Select Case cou_id
	Case "1"
		computerNameprefix="AEDB"
		servername=computerNameprefix&"-NW1"
		connected="True"
	Case "2"
		computerNameprefix="CZOS"
		servername=computerNameprefix&"-NW1"
		connected="False"
	Case "3"
		computerNameprefix="CZPR"
		servername=computerNameprefix&"-NW1"
		connected="True"


Report •

#3
March 13, 2010 at 09:15:02
If connected = false then everything between it and the last end if is skipped.

WScript.Quit is only executed if connected = true AND dn = ""

You could achieve the same thing with a single statement:

dn = CheckForCN(empCN)
If connected = "True" AND dn ="" then WScript.Quit

This would execute a little faster and with a lot of If Then statements can make an appreciable difference.

This is a standard If/.Then construction that exists in one form or another in every programming language.

iF computerNameprefix gets a different value depending on the value of cou_id you have no choice but to have thirty case statement.

However without seeing all the code it is difficult to say what is best.

There are many variations in the case statement:

Case 1,2,3,4 for all values of those numbers

Case 1-10 for everything between and including 1-10

Case Else as a default if non of the previous Case statement apply.

Stuart


Report •

Related Solutions

#4
March 13, 2010 at 09:44:24
Thanks for the advice. It will tidy up the script abit more. Yes, the compnameprefix value is different for all cases as defined by cou_id.

Is it possible to add a line for the connection value, which says something like Case 1-10 connection =true, Case 11-20 connection = false. Don't know if its possible but like:

Select Case cou_id
	Case "1"
		computerNameprefix="AEDB"
		servername=computerNameprefix&"-NW1"
		connected="True"
	Case "2"
		computerNameprefix="CZOS"
		servername=computerNameprefix&"-NW1"
		connected="False"
        Case "1-10"
		connected="True"
        Case "11-20"
		connected="False"

And then I can just order all the True in range 1 and False in range 2 or doesn't it work like this as we are already using the case number higher up?


Report •

#5
March 14, 2010 at 06:37:36
connected="True"
connected="False"

Don't use strings to emulate booleans. Especially since VBScript has booleans:
connected = True
connected = False
If connected Then 
  WScript.Echo "connected = True!"
End If


Report •

#6
March 14, 2010 at 12:25:30
also, you're right, case is exclusionary, so your last post won't work since case ejects after the first "true" condition.
Also, i could not get the dash version:
case 1-10
to work, only the comma version:
case 1,2,3
(i would not use quotes around the test values, that treats as strings)
you could avoid a lot of testing by using arrays:
dim co(20)
co(1)="AEDB"
co(2)="CZOS"
computernameprefix=co(cou_id)
if computernameprefix <>"" then
servername=computerNameprefix&"-NW1"
end if
if cou_id<11 then
connected=true
else
connected=false
end if

or if you don't need to separate name & pre:
co(1)="AEDB-NW1"
co(2)="CZOS-NW1"
servername=co(cou_id)

or if you prefer the case arrangement, i would suggest:
connected=false
Select Case cou_id
Case 1
computerNameprefix="AEDB"
servername=computerNameprefix&"-NW1"
connected=True
Case 2
computerNameprefix="CZOS"
servername=computerNameprefix&"-NW1"
connected=False (?, not sure here...)
Case 3,4,5,6,7,8,9,10
connected=True
case else
connected=false
end select


Report •


Ask Question