Programming Field

Call - DOS/Command Prompt Reference

Used to execute a batch program within a batch program without terminating execution of the current batch program.

Syntax

call <command-line>

[Windows NT series] [Extensions]

call :<label> [<label-param> ...]

Options

<command-line>
Specifies the batch file to execute. Can specify parameters if needed.
:<label>
[Windows NT series] [Extensions] Specifies the label existing in the same batch file. When ‘exit /B’ is executed after jumping to the specified label, the next execution will be the next line of this Call execution.
<label-param> ...
[Windows NT series] [Extensions] You can specify parameters after the label.

Details

Usage of Call

Call is used to execute a batch program. Unlike executing a batch program directly, the caller batch program does not terminate on Call. (Executing a batch program without Call will terminate the caller batch program.)

[Windows NT series] [Extensions] By using extended syntax of Call, like function calls in a program, you can execute into the label (see ‘:’ page) in a batch file, and return back by executing ‘exit /B’. If you specify parameters just like calling a batch program, you can use those parameters by using ‘%1’, ‘%2’, and so on.

Samples

Sample 1

call C:\foo.bat

Executes the batch program C:\foo.bat. When C:\foo.bat exits, the batch program will continue to the next line. (Executing the batch program without Call will terminate the program.)

Sample 2 [Batch file]

@echo off
call :exec_cmd foo.exe %1
call :exec_cmd bar.exe %1
rem To exit the batch program, jump to the end of the program with goto or use exit /b.
goto end

:exec_cmd
echo Execute %1 with the parameter %2 ?
choice /C YN
if %errorlevel%==2 exit /B 0
%1 %2
exit /B

:end

[Windows Vista or later] [Extensions] Executes ‘foo.exe’ and ‘bar.exe’ with the parameter specified to the batch program. Before executing, prompts whether to execute by using Choice command and prevents from executing if N is selected.

This program does not work on Windows XP or earlier due to Choice. (You need to adjust syntax if you use MS-DOS Choice.com.) This program uses Echo instead of /M option in Choice, because /M requires double quotation mark (" ") to specify a message, and ‘%1’ and ‘%2’ may include double quotation marks (possible duplicate double quotation marks).

* Note that executing ‘exit’ without ‘/B’ will cause to terminate the caller batch program, or the caller Command Prompt session.

Sample 3 [Batch file]

@echo off
setlocal
call :get_fullpath FULL_FILENAME "%FILENAME%"
myapp.exe %FULL_FILENAME%
exit /B %errorlevel%

:get_fullpath
    set "%~1=%~f2"
    exit /b 0

[Windows XP or later] [Extensions] To retrieve the full path (the absolute path), based on the current directory, of the file name stored in the environment variable ‘FILENAME’, creates the label ‘get_fullpath’. The subroutine get_fullpath converts the second parameter to the full path by using enhanced parameter substitution of ‘%’, and stores its value to the environment variable named by the first parameter.

* There is no method to retrieve the full path from ‘%FILENAME%’ by using enhanced expansion of ‘%’, so the above sample uses ‘get_fullpath’. If the file name comes from the batch program parameter (such as %1), you can retrieve the full path directly by using something like ‘%~f1’.

See also