Calculation in a .bat script
Titi
-
cs_PaTaTe Posted messages 1471 Registration date Status Contributeur Last intervention -
cs_PaTaTe Posted messages 1471 Registration date Status Contributeur Last intervention -
Good evening everyone,
I would like to perform calculations in a .bat script. Is that feasible? If so, how can I initialize a variable and increment it? Thank you in advance for your help.
TITI
I would like to perform calculations in a .bat script. Is that feasible? If so, how can I initialize a variable and increment it? Thank you in advance for your help.
TITI
2 réponses
no, you cannot
the only way is to integrate GNU tools like expr (unix command) on the machines where the script will run
expr $TOTO + 1 to increment the value of TOTO
the only way is to integrate GNU tools like expr (unix command) on the machines where the script will run
expr $TOTO + 1 to increment the value of TOTO
I dig it up, but to calculate a power in batch we do it like this We create the function !!!! )
Nothing is impossible, your inability to solve a problem does not mean that it's unfeasible!
Nothing is impossible, your inability to solve a problem does not mean that it's unfeasible!
setlocal EnableDelayedExpansion
set /P taille_mdp="the power value to be applied:"
echo password length=!taille_mdp!
set /P var_taille="enter the number to calculate"
cls
:power
for /L %%a in (1,1,!taille_mdp!) do (
set power=!power!*!var_taille!
)
::count number of digits
:count_number_combination
set /A count_power+=1
set var_power=!power:~%count_power%,1!
if not "!var_power!"=="" goto count_number_combination
set /A power_n=!power:~1,%count_power%!
echo !power_n!
pause
exit
Off-topic since this code always returns an integer... I was also talking about calculations with decimal numbers (regardless of the operator), and the batch cannot do that natively! I confirm what I'm saying!
So before talking about the probable inability of others, make sure to read the problem well before giving a solution ^^
No hard feelings :)
So before talking about the probable inability of others, make sure to read the problem well before giving a solution ^^
No hard feelings :)
:: Variable ::
set name=Calculator
set author=MexangaFR
set ver=Alpha 1.0
::::::::::::::
:: Unprotected code and example services for beginners ::
title %name% - %ver% by %author%
:start
color F
cls
echo.
echo [1] Add
echo [2] Subtract
echo [exit] Exit
echo.
echo.
echo.
echo.
set /p choice=Your choice:
IF %CHOICE% equ 1 goto add
IF %choice% equ 2 goto sub
IF %choice% equ exit exit
IF NOT DEFINED CHOICE goto error
:error
color C
cls
echo.
echo [1] Add
echo [2] Subtract
echo [exit] Exit
echo.
echo ::::::An error occurred:::::
echo ::::::Error1: %choice% is not available::::::
echo.
set /p choiceerror=Your choice:
IF %CHOICEERROR% equ 1 goto add
IF %choiceerror% equ 2 goto sub
IF %choiceerror% equ exit exit
IF NOT DEFINED CHOICE goto error2
:error2
color C
cls
echo.
echo [1] Add
echo [2] Subtract
echo [exit] Exit
echo.
echo ::::::An error occurred:::::
echo ::::::Error1: %choiceerror% is not available::::::
echo.
set /p choiceerror=Your choice:
IF %CHOICEERROR% equ 1 goto add
IF %choiceerror% equ 2 goto sub
IF %choiceerror% equ exit exit
IF NOT DEFINED CHOICE goto error
goto start
:add
CLS rem Added after the video
echo.
set /p add1=Which number do you want to add?
set /p add2=And?
set /a add = %add1% + %add2%
echo Result: %add1% + %add2% = %add%
echo.
echo [2] Subtract
echo [return] Go back to the beginning
echo [exit] Exit
echo.
set /p choiceadd=:
IF %CHOICEADD% EQU 2 (cls & goto sub)
IF %CHOICEADD% EQU return (cls & goto start)
IF %CHOICEADD% equ exit exit
IF NOT DEFINED CHOICE goto start
goto start
:sub
cls rem Added after the video
echo.
set /p sub1=Which number do you want to subtract?
set /p sub2=And?
set /a sub = %sub1% - %sub2%
echo Result: %sub1% - %sub2% = %sub%
echo.
echo [2] Add
echo [return] Go back to the beginning
echo [exit] Exit
echo.
set /p choicesub=:
IF %CHOICEADD% EQU 1 (cls & goto sub)
IF %CHOICEADD% EQU return (cls & goto start)
IF %CHOICEADD% equ exit exit
IF NOT DEFINED CHOICE goto start