Calculating the sum of two variables in a batch script
You need to use the property /a
on the set command.
For example,
set /a "c=%a%+%b%"
This allows you to use arithmetic expressions in the set command, rather than simple concatenation.
Your code would then be:
@set a=3
@set b=4
@set /a "c=%a%+%b%"
echo %c%
@set /a "d=%c%+1"
echo %d%
and would output:
7
8
According to this helpful list of operators [an operator can be thought of as a mathematical expression] found here, you can tell the batch compiler that you are manipulating variables instead of fixed numbers by using the += operator instead of the + operator.
Hope I Helped!
You can solve any equation including adding with this code:
@echo off
title Richie's Calculator 3.0
:main
echo Welcome to Richie's Calculator 3.0
echo Press any key to begin calculating...
pause>nul
echo Enter An Equation
echo Example: 1+1
set /p
set /a sum=%equation%
echo.
echo The Answer Is:
echo %sum%
echo.
echo Press any key to return to the main menu
pause>nul
cls
goto main
@ECHO OFF
TITLE Addition
ECHO Type the first number you wish to add:
SET /P Num1Add=
ECHO Type the second number you want to add to the first number:
SET /P Num2Add=
ECHO.
SET /A Ans=%Num1Add%+%Num2Add%
ECHO The result is: %Ans%
ECHO.
ECHO Press any key to exit.
PAUSE>NUL