&&我们在批处理中经常要遇到数字(值)的处理,下面我就简单讲一下批处理中数字(值)处理方法:
一、随机数
在系统变量中有一个随机取值的变量%random%,其为0-32767之间的十进制数字,利用这个变量我们就能取得我们想要的任一组随机数字(等下会说明方法)。
二、四则运算
如我们要对一个变量(数值)进行运算,则可使用set /a命令,如:set /a str+=1就是把变量str的数值加上1,同样只要把这个命令中的+改成-、*、/就可以完成对该数值的减1、乘1、除1运算了,而更重要的是我们可以通过set /a命令来进行四则运算,如:set /a str=5*6+4*3-2*7,也可以进行变量和变量间的四则运算,如:set /a str=%a%/%b%*%c%-%d%,但要注意一点:运算仅限于整数,如要对小数进行运算可先乘上10的n次方。
三、取余
所谓余数就是除数除以被除数剩下的值,在批处理中取余运算符是用%%来表示的,如:%random%%%56就是把不断地把随机数除以56取余数,得到的数值一定是处在0-55间的,那么我们只要set /a a=%random%%%56就能把变量a的值设定在0-55间(大家想一下为什么a不会等于56),如要把a设定在1-56间,则只需set /a a=%random%%%56+1,我们了解了这点是很重要的,如要随机设定一台机器的ip(假设4个数值都随机取定)则只需写下以下代码(设置部分略去):
@echo off
set /a a=%random%%%256,b=%random%%%256,c=%random%%%256,d=%random%%%256
set ip=%a%.%b%.%c%.%d%
netsh interface...
四、去零
数值去零一般是运用在时间计算上,因时间显示一般都是两位不足两位的自动在个位前补零,如07:04:01,这就给我们在运算中带来麻烦,所以对时间进行运算应该先去零,方法其实也很简单,就是利用取余运算,如要对08去零则只在08前面加上10变成108再不断地除以10余数肯定是8,如时间变量a(值为08)的运算公式为:set /a a=10%a%%%10,但时间是有两位数的,因此要将上面的公式变为set /a a=100%a%%%100,现在我们对上面的07:04:01进行取零,代码如下:
@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>nul
最后显示的结果就是7:4:1。
五、递加(减、乘、除)运算
在批处理中我们经常要用到递加(减、乘、除)的运算,那怎么实现呢?只有通过循环来实现,有两种循环的方法:一种是for循环,一种是goto循环。如我们要对一个数值5进行递加100次变成105,两种方法的代码如下:
1.for循环
@echo off
set n=5
for /l %%i in (1,1,100) do set /a n+=1
echo %n%&pause>nul
2.goto循环
@echo off
set n=5
:begin
set /a n+=1
if %n% neq 105 goto begin
echo %n%&pause>nul
同理可实现递减、乘、除和重复四则运算。
我也就简单讲这么多了,主要是想给初学批处理的新手以自己微薄的帮助,还望各位高人予以补充和指教。
Last edited by
zw19750516 on 2008-6-23 at 06:04 PM ]
&&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):
@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>nul
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
@echo off
set n=5
for /l %%i in (1,1,100) do set /a n+=1
echo %n%&pause>nul
2. goto loop
@echo off
set n=5
:begin
set /a n+=1
if %n% neq 105 goto begin
echo %n%&pause>nul
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 ]