## Doing MOD calculations in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
rondinardo
Posts: 1
Joined: 14 Aug 2007 08:56

### Doing MOD calculations in batch

I would love to do a modulo in batch ... does anyone know how to write it??

I figure it's something simlar to ADDITION:

set /a var1=4 + 2

So I was thinking of the logical choices:

set /a var2=4 % 2

- or -

set /a var2=4 MOD 2

Neither of these work. What's wrong? Am I at least close?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54
rondinardo,

Since the % sign is also used for variablen substitution you'll need to use two of them to make the commandline parser understand it. Try:

Set /a a = 13 %% 5

You should get 3.
DOS IT HELP

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

### Re: Doing MOD calculations in batch??

Set /a a = 13 %% 5
giving error missing operand

OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601

OS Name: Microsoft Windows Server 2008 R2 Standard
OS Version: 6.1.7601 Service Pack 1 Build 7601

Squashman
Expert
Posts: 4129
Joined: 23 Dec 2011 13:59

### Re: Doing MOD calculations in batch??

pp8771 wrote:
10 Jul 2018 08:03
Set /a a = 13 %% 5

If you are running from a command prompt you use one percent symbol.

Code: Select all

``Set /a  a = 13 % 5``
If you are running the code in a batch file then use two percent symbols.

Code: Select all

``Set /a  a = 13 %% 5``
Look at Dave's explanation here.

pp8771
Posts: 15
Joined: 12 Jun 2018 06:54

### Re: Doing MOD calculations in batch??

Now working

Aacini
Expert
Posts: 1623
Joined: 06 Dec 2011 22:15
Location: México City, México

### Re: Doing MOD calculations in batch??

It is working here. This is my test:

Code: Select all

``````Microsoft Windows [Versión 6.3.9600]
(c) 2013 Microsoft Corporation. Todos los derechos reservados.

C:\Users\Antonio> cd test

C:\Users\Antonio\Test> type test.bat
@echo off

set /a  a = 13 %% 5

echo Result: %a%

C:\Users\Antonio\Test> test
Result: 3
``````
Antonio