BATCH SET unable to use divide operator

5,934

This can happen if your extenstions are disabled. You can check setlocal /? and cmd /? for more information about extensions.

But the set /? specifies:

If Command Extensions are enabled SET changes as follows:  
...
Two new switches have been added to the SET command:  
  SET /A expression  
  SET /P variable=[promptString]

So without extensions the /A does not work.

You can add the setlocal-line to the top of your batchfile:

setlocal enableextensions enabledelayedexpansion
SET /A 216/18
SET /A div=216/18
ECHO %div%
Share:
5,934

Related videos on Youtube

Gen1n
Author by

Gen1n

A render wrangler in an animation company based in India, trying to hone my skills in programming. Update: Associate Innovator in an Australian based consulting company.

Updated on September 18, 2022

Comments

  • Gen1n
    Gen1n over 1 year

    I am unable to divide using the divide operator provided in the SET command.

    SET /A 216/18

    SET /A div=216/18

    ECHO %div%

    Though this should work and it does in the command prompt window but when used through a batch script it gives a blank output.

    like

    ECHO is on.

    Whats wrong?

    • Christian
      Christian over 10 years
      What means blank? For me, the script works.
    • Christian
      Christian over 10 years
      Have you added a "Pause"? I have written the code in notepad++ and it works as bat-script.
    • martineau
      martineau over 10 years
      Works for me in a command prompt window.
    • BillR
      BillR over 10 years
      File testbat.bat CD C:\DeleteLater SET /A Div=216/3 ECHO /3 %Div% >> testbat.txt File testbat.txt /3 72
    • BillR
      BillR over 10 years
      Substitute SET Div >> testbat.txt yields Div=72 in testbat.txt
    • Gen1n
      Gen1n over 10 years
      UPDATE: It works in my friend's PC as well but as soon as I import the same script to my computer and run it, it doesn't work. We both share the same version of Windows and almost same config but it shouldn't matter.