CALL – Windows CMD Command

Call one batch program from another, or call a subroutine.

The Microsoft help for the CALL command rather misleadingly states “Calls one batch program from another without stopping the parent batch program” it is true that the parent does not STOP, but it does PAUSE while the second script runs.

CALL a second batch file

The CALL command will launch a new batch file context along with any specified parameters. When the end of the second batch file is reached (or if EXIT is used), control will return to just after the initial CALL statement.

In many cases you will also want to use SETLOCAL and ENDLOCAL to keep variables in different batch files completely separate, this will avoid any potential problems if two scripts use the same variable name.

If you execute a second batch file without using CALL you may run into some buggy behavior: if both batch files contain a label with the same name and you have previously used CALL to jump to that label in the first script, you will find the execution of the second script starts at the same label. Even if the second label does not exist this will still raise an error “cannot find the batch label”. This bug can be avoided by always using CALL.

CALL a subroutine (:label)

The CALL command will pass control to the statement after the label specified along with any specified parameters.
To exit the subroutine specify GOTO:eof this will transfer control to the end of the current subroutine.

A label is defined by a single colon followed by a name. This is the basis of a batch file function.

Passing by Reference

In addition to passing numeric or string values on the command line, it is also possible to pass a variable name and then use the variable to transfer data between scripts or subroutines. Passing by reference is a slightly more advanced technique but can be particularly useful when the string contains characters that are CMD delimiters or quotes.

Buggy behavior when using CALL

Redirection with & | <> does not work as expected.
If the CALL command contains a caret character within a quoted string “test^ing”, the carets will be doubled.

Advanced usage : CALLing internal commands

CALL can also be used to run any internal command (SET, ECHO, etc) with the exception of FOR and IF.
CALL will expand any variables passed on the same line. CALL REM only partly works: redirection operators, conditional execution operators, and brackets will be not remarked.

This is undocumented behavior, in fact whenever CALL is run without a: prefix, it will always search disk for a batch file/executable called command before running the internal command. The effect of this extra disc access is that CALL SET is significantly slower than CALL, its use in loops or with a large number of variables should be avoided.

The line shown in bold has the ‘%’ symbols tripled, CALL will expand this to: SET _result=frodo

Each CALL does one substitution of the variables. (You can also do CALL CALL… for multiple substitutions)

Errorlevels

If you run CALL SET this will reset ERRORLEVEL = 0 even though normally SET … will fail to reset an ERRORLEVEL
If you CALL a subroutine, the ERRORLEVEL will be left unchanged
If you CALL a subroutine, with a label that does not exist ERRORLEVEL will be set to 1

If you CALL an executable or resource kit utility make sure it’s available on the machine where the batch will be running, test for its existence with an IF command, and throw an error if missing.

CALL is an internal command, (internally it is closely related to GOTO).
If Command Extensions are disabled, the CALL command will not accept batch labels.

You may also like...

1 Response

  1. zortilo nrel says:

    I’m truly enjoying the design and layout of your website. It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Great work!

Leave a Reply

Your email address will not be published. Required fields are marked *