Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating remainder in batch?

Tags:

batch-file

When entered manually into the commnand prompt, the following sequence works as desired:

>set /a r=3%2
1
>echo %r%
1

However, when launched from batchh file, it doesn't, as %2 clearly has a different meaning.

>set /a r=3%2

>echo %r%
3

How do I make the batch file do what I did in the first example, claculating the remainder? The system in question is XP.

like image 753
Kravaros Avatar asked Dec 07 '13 13:12

Kravaros


People also ask

What does %I mean in a batch command?

%%i is simply the loop variable. This is explained in the documentation for the for command, which you can get by typing for /? at the command prompt. The fact that a double percent sign is used in a batch file is discussed in these links: What is the difference between % and %% in a cmd file?

How do you repeat a batch file?

Pressing "y" would use the goto command and go back to start and rerun the batch file. Pressing any other key would exit the batch file.

What is batch command in CMD?

A batch file is a script file that stores commands to be executed in a serial order. It helps automate routine tasks without requiring user input or intervention. Some common applications of batch files include loading programs, running multiple processes or performing repetitive actions in a sequence in the system.


1 Answers

use this syntax in batch-files:

set /a "r=3 %% 2"
like image 148
Stephan Avatar answered Oct 21 '22 20:10

Stephan