Many developers coming from modern programming languages are surprised to find that Windows batch scripting doesn't handle arithmetic expressions natively. When you try something basic like:
echo 4+2
You'll literally get "4+2" as output rather than the expected "6". This isn't a limitation of your understanding - batch files simply don't evaluate expressions directly in this way.
The classic way to perform calculations in batch scripts is using the set /a
command:
set /a result=4+2
echo %result%
This will correctly output "6". The /a
switch tells SET to perform arithmetic evaluation of the expression.
For more complex operations, you can chain multiple calculations:
set /a "value=(10*2)+(15/3)-5"
echo %value%
Batch supports these operators in order of precedence:
- () - grouping
- * / % - multiplication, division, modulus
- + - - addition and subtraction
You can mix variables and literals in expressions:
set num1=5
set num2=3
set /a total=num1*num2
echo %total%
Note that you don't need the % symbols when referencing variables in set /a
expressions.
For quick calculations where you don't need to store the result, use:
cmd /c exit /b %((10*5)+2)%
echo %errorlevel%
This outputs "52" through the errorlevel mechanism.
Batch math has some notable constraints:
- Only integer math (no floating point)
- 32-bit signed integers (-2147483648 to 2147483647)
- No built-in power/exponent operator
For floating-point operations, you'd need to call external programs or use PowerShell integration.
Here's a basic calculator script:
@echo off
:start
set /p "expr=Enter expression: "
set /a result=%expr%
echo Result: %result%
goto start
When working with Windows batch scripting, you might encounter situations where you need to perform mathematical operations. Unlike many modern programming languages, batch scripts don't directly evaluate expressions like 4+2
as you might expect.
The primary way to evaluate expressions in batch scripting is using the SET /A
command. This command performs arithmetic operations and stores the result in a variable:
@echo off
SET /A result=4+2
echo %result%
This will correctly output 6
to the console.
The SET /A
command supports various arithmetic operators:
SET /A addition=5+3 :: 8
SET /A subtraction=5-3 :: 2
SET /A multiplication=5*3 :: 15
SET /A division=10/3 :: 3 (integer division)
SET /A modulus=10%%3 :: 1 (note the double %)
You can also use variables in your expressions:
@echo off
SET num1=4
SET num2=2
SET /A sum=num1+num2
echo The sum is %sum%
Batch scripting supports bitwise operations through SET /A
:
SET /A "bitwise_and=5 & 3" :: 1
SET /A "bitwise_or=5 | 3" :: 7
SET /A "bitwise_xor=5 ^ 3" :: 6
SET /A "shift_left=5 << 1" :: 10
SET /A "shift_right=5 >> 1" :: 2
If you need to evaluate an expression without storing the result, you can use this technique:
@echo off
for /f %%i in ('echo %= exit /b 4+2 %') do echo %%i
While SET /A
is powerful, it has some limitations:
- Only integer arithmetic is supported
- Floating-point operations aren't available
- Complex expressions might require multiple steps
For more complex mathematical needs, consider:
:: Using PowerShell from batch
for /f %%i in ('powershell 4+2') do echo %%i
:: Using VBScript
echo WScript.Echo(4+2) > temp.vbs
for /f %%i in ('cscript //nologo temp.vbs') do echo %%i
del temp.vbs