Skip to main content

How to Use the Modulo Operator (Get the Remainder) in Batch Script

The modulo operation is a fundamental concept in programming that finds the remainder of a division. For example, 10 divided by 3 is 3 with a remainder of 1; therefore, 10 modulo 3 is 1. This is an incredibly useful tool for tasks like determining if a number is odd or even, cycling through a set of options, or checking if a number is an exact multiple of another.

This guide will teach you how to use the modulo operator in Windows Batch scripting with the SET /A command. You will learn the specific and unusual syntax required for the operator in a script and see practical examples of how to apply it to solve common problems.

The Core Command: SET /A

All arithmetic operations in batch scripting are handled by the SET /A command. It provides a full-featured integer math parser that includes addition, subtraction, multiplication, division, and the modulo operator.

The Modulo Operator Syntax (%%)

This is the most critical and non-obvious part of using the modulo operator in a batch script. A single percent sign (%) is reserved for expanding variables (e.g., %MyVar%). To tell the batch interpreter that you literally mean the modulo operator, you must use a double percent sign (%%).

Syntax: SET /A "result = number1 %% number2"

  • SET /A: The command to perform the calculation.
  • %%: The modulo operator.

On the Command Line: If you are typing this command directly into an interactive cmd.exe window (not in a .bat file), you would use a single %. This distinction is crucial.

Basic Example: Calculating a Remainder

This script demonstrates the basic usage by calculating 10 modulo 3.

@ECHO OFF
SET "Dividend=10"
SET "Divisor=3"
SET "Remainder="

REM Use the %% operator to calculate the remainder.
SET /A "Remainder = %Dividend% %% %Divisor%"

ECHO %Dividend% divided by %Divisor% is...
ECHO The remainder is: %Remainder%

Output:

10 divided by 3 is...
The remainder is: 1

Practical Use Case 1: Checking if a Number is Odd or Even

This is the classic textbook example of the modulo operator. A number is even if it is perfectly divisible by 2 (meaning the remainder is 0), and odd otherwise.

@ECHO OFF
SET "MyNumber=42"

SET /A "result = %MyNumber% %% 2"

IF %result% EQU 0 (
ECHO The number %MyNumber% is EVEN.
) ELSE (
ECHO The number %MyNumber% is ODD.
)

Practical Use Case 2: Cycling Through an Array of Options

This is a more advanced and powerful use case. Imagine you have a list of tasks and you want to assign them to a fixed number of workers or servers in a round-robin fashion. The modulo operator is perfect for this.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

REM --- Define our "array" of tasks ---
SET "tasks[0]=ProcessLogs"
SET "tasks[1]=BackupDB"
SET "tasks[2]=RunReports"
SET "tasks[3]=SendEmails"
SET "tasks[4]=CleanupTemp"
SET "TaskCount=5"
SET "WorkerCount=3"

ECHO --- Assigning tasks to workers ---
SET /A "LastIndex = %TaskCount% - 1"
FOR /L %%i IN (0,1,%LastIndex%) DO (

REM --- The modulo operation determines the worker ID ---
SET /A "WorkerID = %%i %% %WorkerCount%"

ECHO Assigning task "!tasks[%%i]!" to Worker #!WorkerID!.
)
ENDLOCAL

In the output is shown that the tasks are assigned sequentially to workers 0, 1, and 2, and then the pattern repeats.

--- Assigning tasks to workers ---
Assigning task "ProcessLogs" to Worker #0.
Assigning task "BackupDB" to Worker #1.
Assigning task "RunReports" to Worker #2.
Assigning task "SendEmails" to Worker #0.
Assigning task "CleanupTemp" to Worker #1.

Common Pitfalls and How to Solve Them

Problem: Forgetting the Double Percent (%%)

This is the most common syntax error. If you use a single % in a script, the parser will get confused, often trying to find a variable that doesn't exist.

Solution: Always remember that inside a .bat file, the modulo operator is %%.

Problem: The Input is Not a Number

If you try to use a non-numeric string with SET /A, the command will fail.

Example of script with error:

SET /A "result = abc %% 2"
Invalid number. Numeric constants are either decimal (17), hexadecimal (0x11), or octal (021).

Solution: Before performing the operation, you should first validate that your variables contain valid numbers.

Problem: Division by Zero

Attempting to perform a modulo operation with a divisor of 0 is a mathematical impossibility and will cause SET /A to fail.

Example of script with error:

SET /A "result = 10 %% 0"
Divide by zero.

Solution: Always add a check to ensure your divisor is not zero before you attempt the operation.

IF %Divisor% NEQ 0 (
SET /A "Remainder = %Dividend% %% %Divisor%"
) ELSE (
ECHO [ERROR] Cannot perform a modulo operation with a divisor of zero.
)

Conclusion

The modulo operator is a powerful tool for integer arithmetic, and SET /A gives you full access to it within your batch scripts.

Key takeaways for using it successfully:

  • All modulo operations are performed with the SET /A command.
  • Inside a batch script (.bat file), the operator must be written as a double percent sign (%%).
  • The most common use cases are for divisibility checks (like odd/even) and for cycling through a fixed number of items.
  • Always validate your inputs to prevent "Invalid number" or "Divide by zero" errors.