Using Batch Files, Part 4: The CALL Statement

Written by Barry Dysert (last updated November 1, 2021)

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." ...


Understanding the NTFS File System

The NTFS file system is at the core of the Windows operating system. This tip explains a bit about it.

Discover More

Capturing a Screen Shot

There are a lot of reasons why you may want to capture screen shots. This tip shows how easy it is to do it.

Discover More

Disabling Automatic Driver Installation

Whenever you plug something into your computer, Windows dutifully tries to find and install the driver for your new ...

Discover More
More WindowsTips

Using Batch Files, Part 3: The IF Command

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

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 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

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}] (all 7 characters, in the sequence shown) 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 8 + 6?

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

Newest Tips