China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-27 07:02
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Introduction to the methods of handling numbers (values) in batch processing: View 1,953 Replies 10
Original Poster Posted 2008-04-18 15:12 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
&&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 ]
Recent Ratings for This Post ( 3 in total) Click for details
RaterScoreTime
huahua0919 +5 2008-04-18 16:18
lxmxn +8 2008-04-19 12:27
pusofalse +6 2008-05-28 23:55
批处理之家新域名:www.bathome.net
Floor 2 Posted 2008-04-18 16:17 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
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 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
bat-zw +5 2008-04-18 20:44
Floor 3 Posted 2008-04-18 20:43 ·  中国 湖南 株洲 电信
金牌会员
★★★★
永远的学习者
Credits 3,105
Posts 1,276
Joined 2008-03-08 13:00
18-year member
UID 112398
Gender Male
Status Offline
Thanks for pointing out, it has been modified.
批处理之家新域名:www.bathome.net
Floor 4 Posted 2008-04-19 12:27 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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.

@echo off

set /a a=1,b=2,c=3,d=4
set /a num=(a+b)*c %% d

echo num is %num%
pause


In addition, the "set /a %a:~0,1%+=1" in floor 2 seems to be an incorrect expression.
Floor 5 Posted 2008-04-19 12:43 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
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


@echo off
set "str=aferefwfwerergrgreaaffwafwa"
set/p= %str% 中有<nul
:loop
set /a %str:~0,1%+=1&set str=%str:~1%&if defined str goto loop
echo a %a% 个
pause
Floor 6 Posted 2008-04-19 12:55 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
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


@echo off
set "str=aferefwfwerergrgreaaffwafwa"
set/p= %str% 中有<nul
:loop ...

This needs to be determined according to the situation. The str in zh159's case is a string variable, so after intercepting, it can assign a value to a sub-string variable. But if your variable a here is a number, won't it cause an error?
Floor 7 Posted 2008-04-19 13:16 ·  中国 江苏 苏州 移动
银牌会员
★★★
Credits 1,608
Posts 780
Joined 2007-10-07 10:19
18-year member
UID 99089
Gender Male
Status Offline
This will of course report an error. Just to explain the variable form of the original poster. It has been corrected.
Floor 8 Posted 2008-05-28 23:09 ·  中国 天津 联通
初级用户
★★
Credits 173
Posts 69
Joined 2007-08-07 22:40
18-year member
UID 94807
Gender Male
Status Offline
Floor 9 Posted 2008-05-29 12:12 ·  中国 广东 广州 电信
银牌会员
★★★
永远的菜鸟
Credits 1,335
Posts 574
Joined 2007-11-27 12:50
18-year member
UID 103929
Gender Male
From 广西
Status Offline
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):
@echo off
set str=07:04:01
for /f "delims=: tokens=1-3" %%a in ("%str%") do (
set /a a=1%%a%%100,b=1%%b%%100,c=1%%c%%100
)
echo %a%:%b%:%c%&pause>nul

But I don't use the modulus method for time calculation. The following is:
@echo off
set aa=12345
:again
set /a ab=%random%%%5
call set times_=%%aa:~%ab%,1%%
:begin
if not "%time:~-3%"==".00" goto begin
set time_b=%time%

:: Insert the code to be run here to calculate its running time
echo.&echo ping -n %times_% 127.1^>nul 的运行时间如下
ping -n %times_% 127.1>nul

:: Calculate the time spent running the code below
set time_e=%time%
set min_b=1%time_b:~3,2%
set sec_b=1%time_b:~6,2%
set min_e=1%time_e:~3,2%
set sec_e=1%time_e:~6,2%
if %sec_e% lss %sec_b% (
if %min_e% lss %min_b% (
set /a sec_e+=60&set /a min_e+=59
) else (
set /a sec_e+=60&set /a min_e-=1
)
)
set /a min_used=%min_e%-%min_b%
set /a sec_used=%sec_e%-%sec_b%
echo.
echo 开始时间:%time_b%
echo 结束时间:%time_e%
echo 运行时间为: %min_used:~-2%分 %sec_used:~-2%.%time_e:~-2%秒
echo.&pause&cls&goto :again
Floor 10 Posted 2008-09-05 15:49 ·  中国 湖北 武汉 电信
初级用户
Credits 40
Posts 17
Joined 2008-08-27 16:32
17-year member
UID 124308
Gender Male
Status Offline
If a 4-digit number may have 1 to 3 leading zeros in front, how to adaptively remove the zeros?
Floor 11 Posted 2008-09-05 16:23 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
```@echo off
set num=0001
set /a num=1000%num%%%10000
echo %num%
pause
```
Forum Jump: