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-26 01:52
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » If we find the maximum value of a set of temperature record values in 1 text and make a judgment View 2,404 Replies 20
Original Poster Posted 2007-05-12 14:41 ·  中国 广东 深圳 电信
初级用户
Credits 190
Posts 40
Joined 2005-03-17 00:00
21-year member
UID 37149
Gender Male
Status Offline
If you want to find the maximum value of a set of temperature records in a text and make a judgment: if it is greater than 45 and less than 90, display OK; otherwise, display NG. The running environment is XP SP2, and it can be achieved with batch processing or VBS scripts. Please help take a look. I tried with FOR but it didn't work. It should be that I don't know how to use it.

The text content is as follows:

--------1.txt---------------

New logging session started at : 04:57:57
Your product Upper Temperature Limit is : 115 C.

04:57:57 | CT : 76 C AT : 0 C
04:58:02 | CT : 76 C AT : 0 C
04:58:07 | CT : 76 C AT : 0 C
04:58:12 | CT : 76 C AT : 0 C
04:58:17 | CT : 76 C AT : 0 C
04:58:22 | CT : 76 C AT : 0 C
04:58:27 | CT : 76 C AT : 0 C
04:58:32 | CT : 76 C AT : 0 C
04:58:37 | CT : 76 C AT : 0 C
04:58:42 | CT : 76 C AT : 0 C
04:58:47 | CT : 76 C AT : 0 C
04:58:52 | CT : 76 C AT : 0 C
04:58:57 | CT : 76 C AT : 0 C
04:59:02 | CT : 76 C AT : 0 C
04:59:07 | CT : 77 C AT : 0 C
04:59:12 | CT : 77 C AT : 0 C
04:59:17 | CT : 83 C AT : 0 C
04:59:22 | CT : 77 C AT : 0 C
04:59:27 | CT : 77 C AT : 0 C
04:59:32 | CT : 77 C AT : 0 C
04:59:37 | CT : 77 C AT : 0 C
04:59:42 | CT : 82 C AT : 0 C
04:59:47 | CT : 78 C AT : 0 C
04:59:52 | CT : 86 C AT : 0 C
04:59:57 | CT : 89 C AT : 0 C
05:00:02 | CT : 90 C AT : 0 C
05:00:07 | CT : 91 C AT : 0 C
05:00:12 | CT : 93 C AT : 0 C
05:00:17 | CT : 93 C AT : 0 C
05:00:22 | CT : 94 C AT : 0 C
05:00:27 | CT : 95 C AT : 0 C
05:00:32 | CT : 95 C AT : 0 C
05:00:37 | CT : 96 C AT : 0 C
05:00:42 | CT : 97 C AT : 0 C
05:00:47 | CT : 97 C AT : 0 C
05:00:52 | CT : 98 C AT : 0 C
05:00:57 | CT : 98 C AT : 0 C
05:01:02 | CT : 99 C AT : 0 C
05:01:07 | CT : 100 C AT : 0 C
05:01:12 | CT : 100 C AT : 0 C
05:01:17 | CT : 102 C AT : 0 C
05:01:22 | CT : 105 C AT : 0 C
05:01:27 | CT : 106 C AT : 0 C
05:01:32 | CT : 108 C AT : 0 C
05:01:37 | CT : 101 C AT : 0 C
05:01:42 | CT : 99 C AT : 0 C
05:01:48 | CT : 101 C AT : 0 C
05:01:53 | CT : 102 C AT : 0 C
05:01:58 | CT : 110 C AT : 0 C
05:02:03 | CT : 112 C AT : 0 C
05:02:08 | CT : 114 C AT : 0 C
05:02:13 | CT : 113 C AT : 0 C
05:02:18 | CT : 114 C AT : 0 C
05:02:23 | CT : 114 C AT : 0 C
05:02:28 | CT : 114 C AT : 0 C
05:02:33 | CT : 116 C AT : 0 C
05:02:38 | CT : 116 C AT : 0 C
05:02:43 | CT : 117 C AT : 0 C
05:02:48 | CT : 118 C AT : 0 C
05:02:53 | CT : 119 C AT : 0 C


-----------------------------------------------------
The longer the temperature monitoring is, the more content there is in the text.

[ Last edited by quan_zhou on 2007-5-12 at 03:44 PM ]
Floor 2 Posted 2007-05-12 15:41 ·  中国 浙江 杭州 联通
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
```@echo off&setlocal enabledelayedexpansion
set a=0
for /f "tokens=5" %%i in (1.txt) do if %%i GTR !a! set a=%%i
if %a% GTR 45 (goto label) else (echo NG)

:label
if %a% LSS 90 (echo OK) else (echo NG)
pause
```
Floor 3 Posted 2007-05-12 15:45 ·  中国 浙江 杭州 联通
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
If the text has

New logging session started at : 04:57:57
Your product Upper Temperature Limit is : 115 C.


Should skip 4 lines.
@echo off&setlocal enabledelayedexpansion
set a=0
for /f "skip=4 tokens=5" %%i in (1.txt) do if %%i GTR !a! set a=%%i
if %a% GTR 45 (goto label) else (echo NG)

:label
if %a% LSS 90 (echo OK) else (echo NG)
pause
Floor 4 Posted 2007-05-12 16:01 ·  中国 广东 深圳 电信
初级用户
Credits 190
Posts 40
Joined 2005-03-17 00:00
21-year member
UID 37149
Gender Male
Status Offline
Thank you very much for brother wudixin96's reply. Your code I tested is OK, thank you! Actually, I have been in contact with DOS for a long time, but I can't go deep into it. It's really useless! Can you help explain your code?
Floor 5 Posted 2007-05-12 16:10 ·  中国 浙江 杭州 联通
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
@echo off&setlocal enabledelayedexpansion
set a=0 /*Used to store the maximum temperature value and initialize*/
for /f "skip=4 tokens=5" %%i in (1.txt) do if %%i GTR !a! set a=%%i /*skip=4 skips 4 lines. Since tokens=5, the temperature value is stored in %%i. You can enter for /? in cmd to learn about the usage of for*/
if %a% GTR 45 (goto label) else (echo NG)

:label
if %a% LSS 90 (echo OK) else (echo NG)
pause

I can't explain anymore. My writing skills are very poor. Sorry ^-^
Floor 6 Posted 2007-05-12 16:15 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```gawk "/CT/{if($5>45&&$5<90){print \"OK\"} else {print \"NG\" }}" 1.txt```
Floor 7 Posted 2007-05-12 16:18 ·  中国 浙江 杭州 联通
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
Is gawk something related to Linux?
Floor 8 Posted 2007-05-12 16:26 ·  中国 新疆 乌鲁木齐 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
set fso=createobject("scripting.filesystemobject")
set op=fso.opentextfile("1.txt",1)
set d=createobject("scripting.dictionary")
on error resume next
for i=1 to 1000
op.skipline
ln=op.readline
if len(ln)=39 then
t=mid(ln,17,2)
end if
if len(ln)=40 then
t=mid(ln,17,3)
end if
t=int(t)
if err=0 then
d.add i,t
end if
next

for each i in d
if d(i)>b then
mx=d(i)
end if
b=d(i)
next

if mx>45 and mx<90 then
msgbox("Temperature is OK"&chr(10)&"The hotest is "&mx&"C")
else
msgbox("Temperature is NG"&chr(10)&"The hotest is "&mx&"C")
end if


VBS version ^^!
你好,脚本专家!
<a target=blank href=tencent://message/?uin=29654761&Site=www.111.com&Menu=yes><img border="0" SRC=http://wpa.qq.com/pa?p=1:29654761:5 alt="点击这里给我发消息"></a>
Floor 9 Posted 2007-05-12 17:06 ·  中国 广东 深圳 电信
初级用户
Credits 190
Posts 40
Joined 2005-03-17 00:00
21-year member
UID 37149
Gender Male
Status Offline
TO zhoushijay Brother
After your code runs, it always shows "The hotest is* C", which is not the maximum temperature value in the text. I tried changing the content in 1.TXT several times, and it's always like that. Thank you everyone for your replies.


TO wudixin96k Brother
Thank you for your explanation. May I ask if GTR in your code means less than and LSS means greater than? Because I tested it separately and it didn't work. For example:
@echo off
set a=99
if %a% GTR 45 goto min
if %a% LSS 90 goto max

:min
Display test less than 45
goto end

:max
Display test greater than 90
goto end

:end
Floor 10 Posted 2007-05-12 17:12 ·  中国 新疆 乌鲁木齐 电信
高级用户
★★★
Autowalk
Credits 845
Posts 375
Joined 2007-03-03 01:58
19-year member
UID 80606
Gender Male
Status Offline
Yeah, I also found the problem and am working hard to fix it.
你好,脚本专家!
<a target=blank href=tencent://message/?uin=29654761&Site=www.111.com&Menu=yes><img border="0" SRC=http://wpa.qq.com/pa?p=1:29654761:5 alt="点击这里给我发消息"></a>
Floor 11 Posted 2007-05-12 17:14 ·  中国 广东 广州 天河区 电信
金牌会员
★★★★
一叶枝头,万树皆春
Credits 2,564
Posts 1,127
Joined 2006-12-25 22:57
19-year member
UID 74552
Gender Male
Status Offline
set /a 1/(var/45)&& set /a 1/(90/var)&& echo %var%
Floor 12 Posted 2007-05-12 17:18 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Originally posted by wudixin96 at 2007-5-12 16:18:
Is gawk something related to Linux?

Linux only integrates this tool. Actually, it can be regarded as a programming language, a professional programming language for text processing. gawk is the GNU version of awk.

To learn more about awk in detail, you can take a look here:
http://www.itisedu.com/phrase/200603021801255.html
Floor 13 Posted 2007-05-12 17:22 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Set fso = CreateObject("Scripting.FileSystemObject")
Set op = fso.OpenTextFile("1.txt",1)
Set d = CreateObject("Scripting.Dictionary")
on Error Resume Next 'Try to use less...
For i To 1000 'It is recommended to use AtEndOfStream... so that the above error ignoring is not needed and it can handle text with more than 1000 lines
op.skipline 'Why skip here before readline?
ln = op.ReadLine
If Len(ln) = 39 Then 'Use len and mid below... It is recommended to learn to use Instr... Otherwise, when the length is not only 2 types...
t = Mid(ln,17,2)
End If
If Len(ln) = 40 Then
t = Mid(ln,17,3)
End If
t = Int(t)
If Err = 0 Then
d.add i,t 'Still the old problem, why not use array d(i)=t, I don't think using Dictionary object has any advantages
End If
Next

For Each i In d 'Using Dictionary, how to become array, sweat~
If d(i) > b Then
mx = d(i)
End If
b = d(i)
Next

If mx > 45 And mx < 90 Then
MsgBox("Temperature is OK" & Chr(10) & "The hotest is " & mx & "C") 'Use constants instead of using functions. Chr(10)=vbCr
Else
MsgBox "Temperature is NG" & Chr(10) & "The hotest is " & mx & "C" 'Don't use parentheses when no return value is needed
End If

'--Lack of releasing objects--
Set d = Nothing
Set op = Nothing
Set fso = Nothing

[ Last edited by slore on 2007-5-12 at 05:36 PM ]
Floor 14 Posted 2007-05-12 17:25 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```gawk "BEGIN{max=0}/CT/{if($5>max)max=$5;if($5>45&&$5<90){print \"OK\"} else {print \"NG\" }}END{print \"The max tep is\",max}" 1.txt```
Floor 15 Posted 2007-05-12 17:30 ·  中国 浙江 杭州 联通
银牌会员
★★★
Credits 1,928
Posts 931
Joined 2007-01-06 11:46
19-year member
UID 75624
Gender Male
Status Offline
EQU - equals
NEQ - not equals
LSS - less than
LEQ - less than or equal to
GTR - greater than
GEQ - greater than or equal to

Check out "if /?" in cmd?
Forum Jump: