Using Batch Files, Part 4: The CALL Statement

by Barry Dysert
(last updated August 4, 2014)

If you hadn't guessed by now, batch files can serve as a rudimentary type of programming language. One thing that modern programming languages provide is the ability to modularize code into small pieces so that if you change one piece you don't risk accidentally changing some unrelated code. You can modularize a large batch file by breaking it into smaller ones and using the CALL statement to invoke the individual modules as required.

Let's take the case of having a menu system which allows the user to invoke any of a number of functions. What we'll do is put each of the functions in their own batch file and use the CALL statement in the main menu batch file to invoke the selected function, which would then return to the menu to allow the user to select another function.

Here's what Menu.bat may look like:

@ECHO OFF
:TOP
CLS
ECHO   Welcome to the Main Menu! Here are your choices...
ECHO.
ECHO A) Perform Function A          B) Perform Function B
ECHO C) Perform Function C          D) Perform Function D
ECHO.
ECHO Q) Quit
ECHO.
CHOICE /C ABCDQ
GOTO %ERRORLEVEL%
:5
GOTO :EOF
:4
CALL FuncD.bat
GOTO TOP
:3
CALL FuncC.bat
GOTO TOP
:2
CALL FuncB.bat
GOTO TOP
:1
CALL FuncA.bat
GOTO TOP

And here's what FuncA.bat may look like:

ECHO This is Function A
PAUSE

Despite the fact that nothing interesting happens here, it's the start of a decent framework for a command-based menu system. And there are a few items that should be noted. Working from top to bottom, the first new command we come across is the CLS command. This simple command just clears the screen so that our menu will always come out looking good by itself. The discerning eye will also notice some ECHO statements that are immediately followed by a period. This is how you output a blank line. Another new item is the built-in :EOF label. This dispenses with the need of having to declare your own label that exists as the last line of the file (like my ":Done" label used in other tips).

This takes us to the CALL statements. The parameter specified with CALL is the name of the batch file that is to be invoked next. So, for example, if the user chose Option A from the menu, the batch file would immediately go to the :1 label which would cause FuncA.bat to be invoked next. My FuncA.bat file simply ECHOs a line and does a PAUSE. The PAUSE statement causes a line to be output which says "Press any key to continue . . .". It then suspends the execution of the batch file until the user presses a key. Once a key is pressed, the batch file proceeds, which in this case means that FuncA.bat terminates and control resumes back in the main menu batch file at the line immediately following the call to FuncA.bat. So the next thing to happen will be the GOTO TOP, which starts everything all over again.

Note that you don't have to put another "@ECHO OFF" at the top of FuncA.bat because the ECHO state is maintained when FuncA.bat is called.

 This tip (13187) applies to Windows 7 and 8.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Restoring the Built-in Windows Libraries

Windows comes with four built-in libraries. You may accidentally (or on purpose) delete one or more of them. This tip tells ...

Discover More

Moving the Pictures Library

The Pictures folder is one of several system libraries created in Windows by default. This library is specifically optimized ...

Discover More

Xplorer2

If Windows Explorer or File Explorer leaves you wanting more from a file management utility, you might want to give Xplorer2 ...

Discover More
More WindowsTips

PowerShell Input and Output

When dealing with a scripting language like PowerShell, one of the first things you need to learn is how to get data into and ...

Discover More

Creating a Simple Batch File

One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide variety of ...

Discover More

Running a Batch File at a Scheduled Time

Once you become comfortable with batch files, chances are that you'll want to use them to perform various system maintenance ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 7 - 0?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.