Calculation in a .bat script

Titi -  
cs_PaTaTe Posted messages 1471 Registration date   Status Contributor 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

2 answers

  1. floxi Posted messages 153 Status Member 94
     
    Hello,
    you don't even need to initialize your variables, you just do it like this:
    @echo off set /a variable=(2*5)/2 echo The result is %variable% pause


    You can also prioritize your calculations using parentheses.

    If you want more info, check out [Advert removed] Moderation CCM
    Good luck

    Flox
    12
    1. X260
       
      And what about the powers??
      0
    2. yaourt39
       
      @echo off 
      :: 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
      1
  2. gfdgdf
     
    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
    2
    1. grofwa Posted messages 440 Status Member 479
       
      It all depends...
      If you're working under Win2k or XP, you can use the extended command:

      SET /a toto=2+3 ECHO %toto%

      will return 5

      Michael.

      Don't worry about being unnoticed; rather seek to do something remarkable [Confucius]
      0
    2. cs_PaTaTe Posted messages 1471 Registration date   Status Contributor Last intervention   503
       
      All of this is only applicable to integers; once you have decimal numbers, Unix tools seem essential to me. Unless there are solutions, but I don't know them ^^
      0
    3. poparnassus Posted messages 462 Status Member 31
       
      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!


      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
      0
    4. cs_PaTaTe Posted messages 1471 Registration date   Status Contributor Last intervention   503
       
      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 :)
      0