中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-28 08:30
47,813 topics / 349,918 posts / today 0 new / 48,277 members
DOS批处理 & 脚本技术(批处理室) » Introduction to the methods of handling numbers (values) in batch processing:
Printable Version  2,206 / 10
Floor1 bat-zw Posted 2008-04-18 15:12
金牌会员 Posts 1,276 Credits 3,105
&&We often encounter the processing of numbers (values) in batch processing. Now I will briefly talk about the methods for processing numbers (values) in batch processing:

1. Random numbers
There is a variable %random% in the system variables that takes random values. It is a decimal number between 0 and 32767. We can use this variable to obtain any set of random numbers we want (the method will be explained later).

2. Four arithmetic operations
If we want to perform operations on a variable (numerical value), we can use the set /a command. For example: set /a str+=1 is to add 1 to the value of variable str. Similarly, just change the + in this command to -, *, / to complete the operations of subtracting 1, multiplying by 1, and dividing by 1 for the value. More importantly, we can use the set /a command to perform four arithmetic operations. For example: set /a str=5*6+4*3-2*7, and we can also perform four arithmetic operations between variables. For example: set /a str=%a%/%b%*%c%-%d%, but we should note that the operations are only for integers. If we want to perform operations on decimals, we can first multiply by 10^n.

3. Remainder
The so-called remainder is the remaining value when the divisor is divided by the dividend. In batch processing, the remainder operator is represented by %%'. For example: %random%%%56 is to continuously divide the random number by 56 and take the remainder. The obtained value must be between 0 and 55. Then we can set /a a=%random%%%56 to set the value of variable a between 0 and 55 (think about why a will not be equal to 56). If we want to set a between 1 and 56, we just need to set /a a=%random%%%56+1. It is very important for us to understand this point. For example, if we want to randomly set an IP address of a machine (assuming that all 4 values are randomly taken), we just need to write the following code (the setting part is omitted):



4. Removing leading zeros
Numerical leading zero removal is generally used in time calculation. Because the time display is generally two digits, and if it is less than two digits, a zero is automatically added in front of the units are blocked by the strong. For example, 07:04:01, which brings trouble to our operations. Therefore, we should first remove the leading zeros for time calculation. The method is actually very simple, that is, using the remainder operation. For example, to remove the leading zero for 08, just add 10 in front of 08 to become 108 and continuously divide by 10, the remainder must be 8. For example, the calculation formula for time variable a (with a value of 08) is: set /a a=10%a%%%10, but the time has two digits, so the above formula should be changed to set /a a=100%a%%%100. Now we remove the leading zeros for the above 07:04:01. The code is as follows:


The final displayed result is 7:4:1.

5. Increment (decrement, multiplication, division) operations
We often need to use increment (decrement, multiplication, division) operations in batch processing. How to implement them? Only through loops. There are two methods of loops: one is the for loop, and the other is the goto loop. For example, if we want to increment the value 5 by 100 times to become 105, the codes of the two methods are as follows:

1. for loop



2. goto loop



Similarly, decrement, multiplication, division and repeated four arithmetic operations can be realized.

I just talk about so much. Mainly, I want to give a little help to novice batch processing beginners. I also hope that all experts will supplement and give instructions.

[ Last edited by zw19750516 on 2008-6-23 at 06:04 PM ]
Floor2 huahua0919 Posted 2008-04-18 16:17
银牌会员 Posts 780 Credits 1,608
But we need to pay attention to two points: 1. The variable on the left of the = sign does not add the % symbol, while the variable on the right must add it.
set a=abc
set /a %a:~0,1%+=1

[ Last edited by huahua0919 on 2008-4-19 at 01:17 PM ]
Floor3 bat-zw Posted 2008-04-18 20:43
金牌会员 Posts 1,276 Credits 3,105
Thanks for pointing out, it has been modified.
Floor4 lxmxn Posted 2008-04-19 12:27
版主 Posts 4,938 Credits 11,386
A good basic tutorial.

However, there are some points to note:
But two points should be noted: 1. The variable on the left of the = sign generally does not have the % symbol added (there will be expressions like %~a, 2% when getting variables through character truncation), while the variable on the right must be added;

When doing arithmetic operations, the variable on the right does not need to add the % symbol, and adding it will反而 reduce the readability of the expression.



In addition, the "set /a %a:~0,1%+=1" in floor 2 seems to be an incorrect expression.
Floor5 huahua0919 Posted 2008-04-19 12:43
银牌会员 Posts 780 Credits 1,608
Reply:

Back to the moderator, I haven't been exposed to this before, but I suddenly became clear after seeing what zh159 wrote. Now post the code

Floor6 lxmxn Posted 2008-04-19 12:55
版主 Posts 4,938 Credits 11,386
Originally posted by huahua0919 at 2008-4-19 12:43:
Reply to the moderator, I haven't been exposed to this before, but I suddenly became enlightened after seeing what zh159 wrote
Now post the code

Floor7 huahua0919 Posted 2008-04-19 13:16
银牌会员 Posts 780 Credits 1,608
This will of course report an error. Just to explain the variable form of the original poster. It has been corrected.
Floor8 ggyy66 Posted 2008-05-28 23:09
初级用户 Posts 69 Credits 173
Floor9 moniuming Posted 2008-05-29 12:12
银牌会员 Posts 574 Credits 1,335 From 广西
IV. Removing Leading Zeros
Numerical leading zero removal is generally applied in time calculations because time displays are usually two digits, and if less than two digits, a leading zero is automatically added, such as 07:04:01. This causes trouble in calculations, so we should first remove leading zeros when performing operations on time. The method is actually very simple, which is to use the modulus operation. For example, to remove the leading zero from 08, just add 10 in front of 08 to become 108, and then keep dividing by 10, the remainder must be 8. For example, the calculation formula for time variable a (with a value of 08) is: set /a a=10%a%%%10, but since time has two digits, the above formula should be changed to set /a a=100%a%%%100. Now we remove leading zeros from the above 07:04:01...

I originally had some thoughts about this calculation, but I didn't want to say them, but after seeing the LZ's less popular post, I decided to talk about my own opinions. In fact, there is some error in the description of the red part by the building owner. To remove the leading zero from 08, you just need to add 1 in front of 08 and then take the remainder of dividing by 100. That is, the example given is changed to the following, which can be more efficient (although not obviously):

But I don't use the modulus method for time calculation. The following is:
Floor10 mcubird Posted 2008-09-05 15:49
初级用户 Posts 17 Credits 40
If a 4-digit number may have 1 to 3 leading zeros in front, how to adaptively remove the zeros?
Floor11 HAT Posted 2008-09-05 16:23
版主 Posts 5,017 Credits 9,023
```@echo off
set num=0001
set /a num=1000%num%%%10000
echo %num%
pause
```
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023