Using Batch Files, Part 4: The CALL Statement

by Barry Dysert
(last updated November 13, 2017)

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   Welcome to the Main Menu! Here are your choices...
ECHO A) Perform Function A          B) Perform Function B
ECHO C) Perform Function C          D) Perform Function D
ECHO Q) Quit
CALL FuncD.bat
CALL FuncC.bat
CALL FuncB.bat
CALL FuncA.bat

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

ECHO This is Function A

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, 8, and 10.

Author Bio

Barry Dysert

Barry has been a computer professional for over 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...


Using Your Own Picture as an Account Picture

You can easily customize your computing experience by using your own picture as your account picture. This tip explains how.

Discover More

Choosing an Icon Size for the Taskbar

Choosing an icon size for the taskbar is a quick and simple process. This tip explains how it's done.

Discover More

Using Flip 3D

Flip 3D is an Aero feature that allows you to cycle through your active windows in three dimensions. Because they are ...

Discover More
More WindowsTips

Using the SHIFT Statement

Sometimes your batch file needs to accept an unknown number of parameters. This is easy to deal with if you know about ...

Discover More

Using Batch Files, Part 2

This tip is part of a series that talks about Windows batch files. It introduces a few more commands and special ...

Discover More

Using the FOR Statement

In another tip we were introduced to the various FOR loops that exist in Windows. The actual use of these loops was left ...

Discover More

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

View most recent newsletter.


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 9 + 6?

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

Newest Tips

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.