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.
Once you become comfortable with batch files, chances are that you'll want to use them to perform various system ...
Discover MoreThis tip is part of a series that talks about Windows batch files. It introduces a few more commands and special ...
Discover MoreIn another tip we were introduced to the various FOR loops that exist in Windows. The actual use of these loops was left ...
Discover MoreThere are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Copyright © 2024 Sharon Parq Associates, Inc.
Comments