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

MORE FROM BARRY

Determining If a Drive Should be Defragmented

Disk fragmentation is one of those things which, if severe, can adversely affect your system's performance. This tip ...

Discover More

Defragmenting a Hard Drive

As files are added, removed, and edited on a hard drive, the files and the disk itself become fragmented. This causes ...

Discover More

Using Powercfg to List Your Existing Power Schemes

Windows allows you to define different power schemes that control how your computer uses power. This tip shows you how to ...

Discover More
More WindowsTips

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

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

Discover More
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}] (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 two more than 7?

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


Newest Tips