Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Division in batch files?  (Read 54314 times)

0 Members and 1 Guest are viewing this topic.

The Hansenator

    Topic Starter


    Greenhorn

    Division in batch files?
    « on: March 28, 2009, 08:35:04 PM »
    Hello everyone.

    I'm trying to write a batch file for a school assignment and I'm having a heck of a time figuring out how to divide one number by another. I assume it's possible because add, subtract, and multiply seem to work.

    This is the line I'm having trouble with:

    set /a sum1=%num1% / %num2%

    I suppose the computer is confused because the / character is already used but I haven't been able to find how it's done. If anyone can help that would be awesome.

    Carbon Dudeoxide

    • Global Moderator

    • Mastermind
    • Thanked: 169
      • Yes
      • Yes
      • Yes
    • Certifications: List
    • Experience: Guru
    • OS: Mac OS
    Re: Division in batch files?
    « Reply #1 on: March 28, 2009, 08:37:33 PM »
    That should be correct.
    Get rid of the space and see if it works for you.

    The Hansenator

      Topic Starter


      Greenhorn

      Re: Division in batch files?
      « Reply #2 on: March 28, 2009, 08:53:57 PM »
      That should be correct.
      Get rid of the space and see if it works for you.

      Thanks for the suggestion, removing the spaces didn't change anything though.

      I don't get an error message but it doesn't seem to do anything either. I just get whatever is already in the variable and the division line doesn't seem to affect it.

      Carbon Dudeoxide

      • Global Moderator

      • Mastermind
      • Thanked: 169
        • Yes
        • Yes
        • Yes
      • Certifications: List
      • Experience: Guru
      • OS: Mac OS
      Re: Division in batch files?
      « Reply #3 on: March 28, 2009, 09:13:59 PM »
      What are %num1% and %num2%

      They should be something along the lines of:
      set num1=10
      set num2=2

      Dias de verano

      • Guest
      Re: Division in batch files?
      « Reply #4 on: March 29, 2009, 12:48:34 AM »
      You can have as many spaces as you like.
      Code: [Select]
      @echo off
      set num1=15
      set num2=3
      set /a sum1=%num1%    /    %num2%
      echo num1=%num1%
      echo num2=%num2%
      echo sum1=%sum1%
      echo %num1%/%num2%=%sum1%

      Output:

      Code: [Select]
      num1=15
      num2=3
      sum1=5
      15/3=5

      The Hansenator

        Topic Starter


        Greenhorn

        Re: Division in batch files?
        « Reply #5 on: March 29, 2009, 11:53:27 AM »
        I tried your example and it worked perfectly. I played with mine and the results seem inconsistent, I'll get one answer the first time and the correct answer the second time. It seems to have something to do with being in an IF statement, if that makes any sense. This is my file, with a couple extra lines for testing purposes:

        Code: [Select]
        @Echo off

        rem ==== Accept user input =======
        Set /p num1=Enter num1:
        Set /P num2=Enter num2:

        rem ===== Calculate and display =====
        set /a sum1=%num1%+%num2%
        Echo %num1% + %num2% is: %sum1%

        set /a sum2=%num1% - %num2%
        Echo %num1% - %num2% is: %sum2%

        set /a sum3=%num1% * %num2%
        Echo %num1% * %num2% is: %sum3%

        set /a sum4=%num1% / %num2%
        echo %Num1% / %num2% is: %sum4%

        rem ==== Check for division by zero =====
        if %num2% == 0 (
          echo Number not valid, 0 entered
        ) else (
          set /a sum5=%num1% / %num2%
          echo %Num1% / %num2% is: %sum5%
        )

        The last line is the goofy one. The first time I run it, it looks like this:
        Code: [Select]
        Enter num1: 8
        Enter num2:2
        8 + 2 is: 10
        8 - 2 is: 6
        8 * 2 is: 16
        8 / 2 is: 4
        8 / 2 is: 3

        The second time I run the same thing and it looks like this:

        Code: [Select]
        Enter num1: 8
        Enter num2:2
        8 + 2 is: 10
        8 - 2 is: 6
        8 * 2 is: 16
        8 / 2 is: 4
        8 / 2 is: 4

        Dias de verano

        • Guest
        Re: Division in batch files?
        « Reply #6 on: March 29, 2009, 12:13:12 PM »
        In a parenthetical statement, such as a FOR loop or an if statement like this, any variables that are set inside the parentheses, in this case, sum5, will expand to a blank or a previous value if there is one.

        Code: [Select]
        if %num2% == 0 (
          echo Number not valid, 0 entered
        ) else (
          set /a sum5=%num1% / %num2%
          echo %Num1% / %num2% is: %sum5%
        )

        you have to use delayed expansion. This is because of the way cmd.exe expands the variables at run time.

        You have to use this statement (put it at the start just after the @echo off line)

        Code: [Select]
        setlocal enabledelayedexpansion
        and in the parentheses use exclamation marks (exclamation points if you are American)

        like this

        num1 and num2 are OK because they were set before the parentheses but sum5 needs delayed expansion.

        Code: [Select]
        if %num2% == 0 (
          echo Number not valid, 0 entered
        ) else (
          set /a sum5=%num1% / %num2%
          echo %Num1% / %num2% is: !sum5!
        )

        Once you are out of the parentheses you can use % signs again for sum5.

        Watch out for spaces in IF comparisons. They can catch you out. This is better

        Code: [Select]
        if %num2%==0
        This is better still

        Code: [Select]
        if "%num2%"=="0"
        « Last Edit: March 29, 2009, 12:53:32 PM by Dias de verano »

        The Hansenator

          Topic Starter


          Greenhorn

          Re: Division in batch files?
          « Reply #7 on: March 29, 2009, 01:51:43 PM »
          It works, thank you so much!

          I added
          Code: [Select]
          setlocal enabledelayedexpansionat the top and used exclamation marks and it works!