How to Perform Basic Math (Add, Subtract, Multiply, Divide) in Batch Script
Performing simple mathematical calculations is a fundamental requirement for many scripts. You might need to increment a counter, calculate an offset, or determine a total. The standard way to handle math in a Windows Batch script is with the SET command, used with its special /A (Arithmetic) switch.
This guide will teach you the syntax of SET /A and show you how to perform all the basic arithmetic operations. You will also learn about its integer-only limitation and how to control the order of operations with parentheses.
The Core Command: SET /A
The SET /A command tells the command processor to treat the string that follows as an Arithmetic expression. It can evaluate the expression and, optionally, assign the result to a variable.
Syntax: SET /A "VariableName = expression"
VariableName =: This part is optional. If you include it, the result of the calculation is stored inVariableName. If you omit it, the result is simply printed to the command line.expression: The mathematical formula you want to evaluate (e.g.,5 + 3).
Basic Arithmetic Operators
The SET /A command supports all the standard operators you would expect:
| Operator | Description | Example |
|---|---|---|
+ | Addition | 5 + 3 |
- | Subtraction | 10 - 4 |
* | Multiplication | 6 * 7 |
/ | Division | 20 / 5 |
%% | Modulo (Remainder) | 13 %% 5 |
Basic Examples of Math Operations
For these examples, we will store the result in a variable and then ECHO it.
Addition (+)
@ECHO OFF
SET "Apples=5"
SET "Oranges=10"
SET /A "TotalFruit = %Apples% + %Oranges%"
ECHO Total fruit: %TotalFruit%
Output:
Total fruit: 15
Subtraction (-)
@ECHO OFF
SET "TotalItems=100"
SET "ItemsUsed=42"
SET /A "ItemsLeft = %TotalItems% - %ItemsUsed%"
ECHO Items left: %ItemsLeft%
Output:
Items left: 58
Multiplication (*)
@ECHO OFF
SET "Price=10"
SET "Quantity=7"
SET /A "TotalCost = %Price% * %Quantity%"
ECHO Total cost: $%TotalCost%
Output:
Total cost: $70
Division (/)
This is where you must be careful. SET /A only performs integer division.
@ECHO OFF
SET /A "Result = 10 / 3"
ECHO 10 divided by 3 is: %Result%
Output:
10 divided by 3 is: 3
The 0.333... fractional part is completely discarded.
Combining Operations and Using Parentheses
The SET /A command understands the standard mathematical order of operations (multiplication and division are performed before addition and subtraction). You can use parentheses () to control the order explicitly.
@ECHO OFF
ECHO --- Calculating with and without parentheses ---
REM This will calculate 5 * 10 first. (2 + 50) = 52
SET /A "Result1 = 2 + 5 * 10"
ECHO 2 + 5 * 10 = %Result1%
REM The parentheses force 2 + 5 to be calculated first. (7 * 10) = 70
SET /A "Result2 = (2 + 5) * 10"
ECHO (2 + 5) * 10 = %Result2%
Common Pitfalls and How to Solve Them
The Integer-Only Limitation
This is the single biggest limitation of SET /A. It cannot handle decimals or floating-point numbers.
10 / 4will result in2, not2.5.SET /A "Value = 99.5"will cause an error.
Solution: For any math that requires decimal precision, you must delegate the calculation to a more powerful tool like PowerShell.
FOR /F %%N IN ('powershell -Command "10 / 4"') DO SET "Result=%%N"
ECHO The precise result is: %Result%
This PowerShell one-liner correctly returns 2.5.
Division by Zero
If your script attempts to divide by a variable that contains 0, it will crash with a "Divide by zero error."
Solution: Always check if your denominator is zero before performing a division.
IF %TotalItems% EQU 0 (
ECHO Cannot divide by zero.
) ELSE (
SET /A "Average = %TotalCost% / %TotalItems%"
ECHO The average cost is: %Average%
)
Practical Example: A Simple File Counter
This script uses SET /A to increment a counter inside a FOR loop to count the number of text files in a directory.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "count=0"
ECHO --- Counting .txt files ---
FOR %%F IN (*.txt) DO (
REM Increment the counter by 1 for each file found.
SET /A "count+=1"
)
ECHO Found !count! text files in this directory.
ENDLOCAL
DelayedExpansion (!count!) is required here to correctly read the changing value of the count variable inside the loop.
Conclusion
The SET /A command is the simple and effective built-in tool for performing integer arithmetic in a batch script.
Key takeaways:
- The core syntax is
SET /A "Var = expression". - It supports all basic operators:
+,-,*,/, and%%(modulo). - It is strictly for integers and will truncate any decimal results from division.
- For floating-point math, you must use a more powerful tool like PowerShell.
- Always check for a division by zero to prevent your script from crashing.