Batch Scripting Basics for Beginners: Variables, Comments, User Input, and Arithmetic Operations
Batch scripting is one of the easiest ways to automate
tasks in Windows. A batch file contains a series of commands that are executed
by the Windows Command Prompt (CMD). System Administrators, IT Support
Engineers, and System Engineers often use batch files for automation,
troubleshooting, software deployment, and maintenance tasks.
In
this article, we will explore some fundamental concepts of batch scripting,
including displaying output, using comments, working with variables, accessing
environment variables, and performing arithmetic operations.
Displaying Output with ECHO
The echo command is used to display messages on the screen.
echo off echo "Hello World from a batch file!" echo 1234 echo Hello Atul
Output
"Hello World from a batch file!"
1234
Hello Atul
Explanation
- echo off hides the execution of commands.
- echo displays text on the console.
- It is commonly used to provide status messages to users.
Understanding @ Symbol
The @ symbol prevents the current command from being displayed before execution.
//echo off @echo "This is for symbol.bat file" @vol @ver
Explanation
- @echo displays text without showing the command itself.
- vol displays volume information of the current drive.
- ver displays the Windows version.
Sample Output
"This is for symbol.bat file"
Volume in drive C is Windows
Volume Serial Number is XXXX-XXXX
Microsoft Windows [Version 10.0.19045.XXXX]
Working with Variables
Variables are used to store values that can be reused throughout the script.
@echo off rem set filename=echo.bat rem echo The name of the batch file is: %filename% set filename=dir echo The name of the batch file is: %filename%
Explanation
- set creates a variable.
- %filename% retrieves the stored value.
- In this example, the variable contains the value dir.
Output
The name of the batch file is:
Directory of C:\Users\Username
Because %filename% contains the command dir, CMD executes it.
Comments in Batch Files
Comments help document scripts and improve readability.
@echo off
:: This is a comment in a batch file. It will not be executed.
rem This is another way to write a comment in a batch file.
echo This line will be executed, but the comments above will be ignored.
Explanation
There are two ways to write comments:
REM
::
Comments are ignored during execution and are useful for documenting code.
Environment Variables
Windows provides several built-in environment variables that contain system information.
@echo off rem set ::cd %SystemRoot% ::dir echo Below is the cmd path echo %cmdcmdline% ::%ComSpec% echo %ComSpec%
Explanation
- %cmdcmdline%
- Displays the command line that started the current CMD session.
- %ComSpec%
- Displays the path of the Command Prompt executable.
Example Output
Below is the cmd path
C:\Windows\System32\cmd.exe
C:\Windows\System32\cmd.exe
These variables are useful when troubleshooting Windows systems.
Arithmetic Operations
Batch scripting supports basic mathematical calculations using set /a.
@echo off set /a sum = 10+10 echo The sum of 10 and 10 is: %sum% Output The sum of 10 and 10 is: 20 More Arithmetic Examples Addition set /a result=15+5 echo %result% Subtraction set /a result=20-5 echo %result% Multiplication set /a result=5*5 echo %result% Division set /a result=100/10 echo %result% Modulus (Remainder) set /a result=10%%3 echo %result%
These operations are frequently used in automation scripts.
Taking User Input
Batch files can interact with users using set /p.
@echo off rem echo Enter your name rem set /P name= rem echo Hello %name%, welcome to batch scripting! echo Enter the first number: set /P numb1= echo Enter the second number: set /P numb2= set /A sum = %numb1%+%numb2% echo %sum%
Sample Execution
Enter the first number:
10
Enter the second number:
20
30
Explanation
- set /p accepts input from the keyboard.
- Values entered by the user are stored in variables.
- set /a performs arithmetic calculations using those variables.
This technique is commonly used in interactive administration
scripts.
Real-World Usage for System Engineers
Batch scripting is useful for:
- Automating repetitive tasks
- Running maintenance scripts
- Gathering system information
- Managing files and folders
- Creating startup and login scripts
- Troubleshooting Windows systems
- Automating software installation
Even though modern organizations prefer PowerShell for advanced
automation, understanding Batch scripting remains valuable because many legacy
systems still use .bat files.
Conclusion
Batch scripting provides a simple way to automate Windows tasks. By
learning commands such as echo, set, rem, environment variables, arithmetic
operations, and user input handling, you build a strong foundation for Windows
administration and automation.

0 comments:
Post a Comment
For Any Tech Updates, Hacking News, Internet, Computer, Technology and related to IT Field Articles Follow Our Blog.