&&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 ]
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):
@echo off
set /a a=%random%%%256,b=%random%%%256,c=%random%%%256,d=%random%%%256
set ip=%a%.%b%.%c%.%d%
netsh interface...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:
@echo off
set str=07:04:01
for /f "delims=: tokens=1-3" %%a in ("%str%") do (
set /a a=100%%a%%100,b=100%%b%%100,c=100%%c%%100
)
echo %a%:%b%:%c%&pause>nulThe 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
@echo off
set n=5
for /l %%i in (1,1,100) do set /a n+=1
echo %n%&pause>nul2. goto loop
@echo off
set n=5
:begin
set /a n+=1
if %n% neq 105 goto begin
echo %n%&pause>nulSimilarly, 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 ]
Recent Ratings for This Post
( 3 in total)
Click for details
| Rater | Score | Time |
|---|---|---|
| huahua0919 | +5 | 2008-04-18 16:18 |
| lxmxn | +8 | 2008-04-19 12:27 |
| pusofalse | +6 | 2008-05-28 23:55 |
批处理之家新域名:www.bathome.net

