Calling a Procedure

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

In general, VBScript processes each line of code in succession. Procedures are an exception to this rule. Neither subroutines nor functions are run unless they have been specifically invoked somewhere in the script. If a procedure has not been invoked, the procedure is simply skipped, regardless of where it appears within the script.

For example, the following script includes a subroutine embedded in the middle of the code. However, this subroutine is never actually called.

Wscript.Echo "A" Sub EchoLine2    Wscript.Echo "B" End Sub Wscript.Echo "C" 

When the preceding script runs under CScript, the following output appears in the command window. Because the subroutine was never called, the subroutine and all the code inside it was skipped and not run:

A C 

The flow of the script goes like this:

  1. The script runs line 1 and echoes the message "A".
  2. The script parses line 2 and sees that it marks the beginning of a subroutine. Because the subroutine has not been called, the script skips lines 2, 3, and 4.
  3. The script runs line 5, the first line following the end of the subroutine, and echoes the message "C". The script then automatically stops without ever running the subroutine.

To ensure that a subroutine runs, it must be called. This is done using a statement that consists solely of the subroutine name. For example, the following script echoes the message "A", calls the subroutine named EchoLineB, and then echoes the message "C".

Wscript.Echo "A" EchoLine2 Wscript.Echo "C" Wscript.Quit Sub EchoLineB    Wscript.Echo "B" End Sub 

When the preceding script runs under CScript, the following output appears in the command window:

A B C 

The flow of the script goes like this:

  1. The script runs line 1, and echoes the message "A".
  2. The script runs line 2, which is a call to the subroutine named EchoLineB.
  3. The script skips to line 5, where the subroutine begins. It skips all the intervening lines, including line 4, which would have caused the script to stop.
  4. The script runs line 6, the only line within the subroutine, which echoes the message "B".
  5. The script runs line 7, which marks the end of the subroutine. Because the subroutine has ended, control of the script returns to the line immediately following the line (line 2) where the subroutine was called.
  6. The script runs line 3 and echoes the message "C".
  7. The script runs line 4 and stops.

Procedures can be placed anywhere within a script, with no degradation of performance. However, the placement of procedures can affect the ease with which a script can be read and maintained. For more information about placing procedures within a script, see "Scripting Guidelines" in this book.

In the following script, an error-handling procedure is used to display any WMI errors that might occur. Throughout the script the Err object is checked. If the value is anything but 0, this means an error occurred; the ErrorHandler subroutine is called, and the appropriate error message is displayed.

On Error Resume Next Set objWMIService = GetObject("Winmgmts:root\cimv2") If Err <> 0 Then     ErrorHandler End If Set colPrinters = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Printer WHERE Name='TestPrinter'") If Err <> 0 Then     ErrorHandler End If For Each objPrinter in colPrinters     Wscript.Echo objPrinter.Name Next Sub ErrorHandler     Select Case Hex(Err.Number)         Case "80041001"             Wscript.Echo "The call failed."         Case "80041002"             Wscript.Echo "The object could not be found."         Case "80041010"             Wscript.Echo "The specified class is not valid."         Case "8004103A"             Wscript.Echo "The specified object path was invalid."         Case "80041048"             Wscript.Echo "The specified class is not supported."         Case Else             Wscript.Echo "An unknown error occurred."     End Select     Err.Clear End Sub 

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net