Wednesday, March 26, 2008

Passing Parameters

When calling a secondary batch file or subroutine, you will often want the routine to manipulate some data, the data (usually a variable) should be passed as a parameter.

If you CALL an executable or resource kit utility make sure it\'s available on the machine where the batch will be running, also check you have the latest versions of any resource kit utilities.

Jumping to a label

A label is defined by a single colon followed by a name

As this is effectively a subroutine I always prefix the name with s_
e.g.
:s_error_trap

When you jump to a subroutine with CALL, all statements after the label are executed until either the end of the script is reached, or a GOTO :eof command.

GOTO :eof will return back to just after the position where you used CALL.

Don\'t forget you can also pass command line arguments to the :label and refer to these just like the parameters passed to a separate batch file.

For example

@ECHO OFF
SETLOCAL
CALL :s_staff SMITH 100
GOTO s_last_bit

:s_staff
ECHO Name is %1
ECHO Rate is %2
GOTO :eof

:s_last_bit
ECHO The end of the script

No comments: