Doing MOD calculations in batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Doing MOD calculations in batch

#1 Post by rondinardo » 14 Aug 2007 09:05

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

#2 Post by DosItHelp » 18 Aug 2007 18:12

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??

#3 Post by pp8771 » 10 Jul 2018 08:03

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: 4107
Joined: 23 Dec 2011 13:59

Re: Doing MOD calculations in batch??

#4 Post by Squashman » 10 Jul 2018 09:03

pp8771 wrote:
10 Jul 2018 08:03
Set /a a = 13 %% 5
I guess you misunderstood the previous comments in this thread.

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??

#5 Post by pp8771 » 18 Jul 2018 04:44

Now working

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

Re: Doing MOD calculations in batch??

#6 Post by Aacini » 18 Jul 2018 05:57

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

Post Reply