Create interactive menus with Batch files In Windows, scripting is one of the most effective ways to automate tasks, streamline workflows, and improve the user experience in technical environments. Although it may seem like an old-fashioned practice, it's still widely used in system administration, software development, maintenance scripting, and local environment configuration. Here, we'll dive into one of the most comprehensive tutorials you can find on the internet on this topic. We'll delve into the structures, commands, examples, and best practices that will allow you to create powerful, reusable, and maintainable scripts.
This article is intended to thoroughly cover everything you need to know about Batch Files., from creating simple menus to more sophisticated techniques such as using subcommands, error handling, browser integration, parallel command execution, using temporary variables, and customizing the CMD environment. We'll also cover topics such as task scheduling, cross-environment compatibility, and how to export configurations for easier use on different systems or in collaborative development teams.
What is a Batch file and what is it used for?
A Batch file, also known as a BAT or CMD file, It is a plain text file with a .bat or .cmd extension that contains a sequence of commands to be executed by the Windows command interpreter.This console, commonly known as CMD or command prompt, allows you to run these scripts line by line automatically, making it ideal for running repetitive tasks, automating installations, starting work environments, performing backups, etc.
These files may include control structures such as loops, conditionals, subroutines, and variables., making them powerful tools for developers, support technicians, system administrators, or even users with more specific needs.
How a Batch File is Structured
A typical Batch script structure includes:
- @ Echo off: Prevents commands from being displayed on the screen while they are being executed.
- TITLE: Changes the name that appears in the title bar of the CMD window.
- CLS: clears the console before executing the rest of the code.
- SET /p: allows collecting user input.
- GOTO: Redirects execution to a specific label based on the script logic.
Let's look at a basic example:
@echo off title Main Menu :start cls echo --- Options Menu --- echo 1. Run task A echo 2. Run task B echo 3. Exit set /p option=Select an option: if "%option%"=="1" goto taskA if "%option%"=="2" goto taskB if "%option%"=="3" exit goto start :taskA echo Running task A... pause goto start :taskB echo Running task B... pause goto start
Essential commands in batch files
Below we explain the most common and useful commands to develop your scripts with greater flexibility and control:
- IF: evaluates a condition and executes an action if it is true.
- SET: Defines environment variables that can be used in the script.
- CALL: Runs another batch file or subcommand without terminating the current one.
- CHOICE: allows you to choose between multiple options using predefined keys.
- START: Open another CMD window or launch an application.
- TIMEOUT: pauses execution for a specified number of seconds.
- TASKKILL: closes processes from Batch.
Creating an advanced menu with multiple options
One of the advantages of working with Batch is the ability to create interactive menus that allow the user to perform different tasks from a single textual interface. Through the use of dynamic variables, input validation, and subcommands, we can create robust and reusable interfaces.
For example, in business or development projects, menus like these are often included in the source code root to make it easier to configure the environment without relying on external applications.
Integration with Maven, Jetty or browsers
Batch menus can perform sophisticated tasks such as Compiling Maven projects (mvn clean install), deploying locally with Jetty, or even automatically opening a browser after a scheduled wait, to ensure the server is ready.
A practical example:
:run start menu.bat explorer http://localhost:8080/miapp/ 35 call mvn clean install jetty:run goto end
Additionally, you can create subcommands like:
:explorer timeout %3 start iexplore.exe %2 exit 0
Using Variables in Batch Scripts
One of the most powerful tools in Batch is the use of dynamic variables, which allow you to customize the execution of scripts. For example:
SET /p username=Enter your name: ECHO Welcome %username%
You can also perform mathematical operations:
SET /A total=%value1% + %value2%
Variables are defined with SET and are named between percentage signs (for example, %var%)
Subroutines and subcommands to maintain order
When your scripts grow, it's essential to maintain a modular structure. Use tags like :compileProject o :launchServer it allows you call specific sections of the script without repeating code.
Subcommand example:
:compile cd project call mvn clean install goto end
Visual tricks and customization of the CMD environment
Not everything is execution. Aesthetics also matter when you're going to use the script daily. You can change the CMD background and text color with:
color 0A
Where the first digit is the background and the second is the text.
And if you want to avoid the “Press a key to continue” message, you can use:
pause > null
Validations, error handling, and custom messages
A well-made script should include clear checks and messages to prevent errors or respond predictably to unexpected input:
if "%var%"=="" ( echo No valid option entered. pause goto start )
Automation with Batch Files and PowerShell
From Configuration Manager you can also Schedule batch and PowerShell scripts to manage multiple devicesYou can define parameters, validate inputs, get real-time responses, and monitor results. This is key in corporate or devops environments.
Creating and distributing executable files
With external utilities you can convert your .bat files to .exe files, change your icon and protect your code for professional or internal distribution.
Compatibility between Windows systems and portability
A good script can be used interchangeably on Windows XP, 7, 10, or 11, as long as you avoid absolute paths or version-specific commands. Additionally, you can create UNIX versions with .sh scripts that mimic the behavior of .bat files for cross-platform projects.
The flexibility, ease of integration, and complete control over Windows environments make Batch scripts a valuable tool, enabling the creation of interactive menus that simplify repetitive tasks, document processes, and unify workflows, all while reducing dependence on external software.