1

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?

7
  • 1
    What means blank? For me, the script works.
    – Christian
    Commented Oct 9, 2013 at 0:05
  • Blank as in no output or if I direct the output to a text file then it creates an empty text file. Also I agree that it should work as this is the basic syntax but it doesn't and it only works in cmd window. I am using "Windows 7 Professional".
    – Gen1n
    Commented Oct 9, 2013 at 0:17
  • Have you added a "Pause"? I have written the code in notepad++ and it works as bat-script.
    – Christian
    Commented Oct 9, 2013 at 0:20
  • Works for me in a command prompt window.
    – martineau
    Commented Oct 9, 2013 at 0:54
  • File testbat.bat CD C:\DeleteLater SET /A Div=216/3 ECHO /3 %Div% >> testbat.txt File testbat.txt /3 72
    – BillR
    Commented Oct 9, 2013 at 1:29

2 Answers 2

0

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%
0
0

Sorry, but I added the lines and the dos bat file still can't do division.

But, I admit that reading the help on the topic made no sense.

Here is the bat code, and what is wrong?:

:DOS can't do division setlocal enableextensions enabledelayedexpansion

SET hours2record=13 SET minutes2record=05 SET tracklength=46800

set /a seconds = %hours2record% * 3600 + %minutes2record% * 60 echo so far so good. Seconds are %seconds% pause

::SET /A trackdiv=%seconds%/46800 does not work ::SET /A trackdiv=468000/46800 works ::SET /A trackdiv=seconds/46800 does not work either SET /A %seconds%/%tracklength% SET /A trackdiv=%seconds%/%tracklength% & does not work

echo trackdiv variable is calculated as %trackdiv% . This is wrong! 47100/46800 does not equal 1! Stupid dos! pause

setlocal /? pause cmd /?

2
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 19, 2023 at 17:17
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From Review Commented Jan 19, 2023 at 18:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .