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-08-09 17:00
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Execute a batch file when the value read in real time from a txt file meets a condition? View 1,060 Replies 12
Original Poster Posted 2008-09-02 20:33 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
After getting the content value of a txt file in real time and calculating it, if it meets the condition then execute another batch program? Please advise.
Storage directory:d:\record
txt filenames:Ter2008Y4M16D12h.txt
Ter2008Y4M16D13h.txt
Ter2008Y4M16D14h.txt
Ter2008Y4M16D15h.txt
:
: one such file is generated every hour.


The txt format is: ( one line is generated every second as a record, the time interval can be specified, for example one line every 3 minutes,
and each time I need to take the value in the last column of the latest record.)

682 2008-4-16 15:00:00 26.75
683 2008-4-16 15:00:01 26.75
684 2008-4-16 15:00:02 26.75
685 2008-4-16 15:00:03 27.25
686 2008-4-16 15:00:04 27.25
687 2008-4-16 15:00:05 27
688 2008-4-16 15:00:06 27
689 2008-4-16 15:00:07 27.25
690 2008-4-16 15:00:08 26.75
691 2008-4-16 15:00:09 27.5
692 2008-4-16 15:00:10 27.5
693 2008-4-16 15:00:12 26.75
694 2008-4-16 15:00:13 27.5
695 2008-4-16 15:00:14 27.25
696 2008-4-16 15:00:15 27
697 2008-4-16 15:00:16 27
698 2008-4-16 15:00:17 27.25
699 2008-4-16 15:00:18 27.25
700 2008-4-16 15:00:19 27.5
701 2008-4-16 15:00:20 27.25
702 2008-4-16 15:00:21 27.25
703 2008-4-16 15:00:22 27.25

Now what I need is: can a program monitor in real time (or every 3 minutes) and get the value in the last column of the newest txt file, and if the value is greater than 30 then automatically execute another batch program; and every day automatically compress all txt files generated on the previous day under d:\record
into another directory, stored as a compressed file named with the previous date. For example: d:\rardata\%date%-1.rar

[ Last edited by qwertl on 2008-9-2 at 08:54 PM ]
Floor 2 Posted 2008-09-02 20:58 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
qwertl +2 2008-09-12 22:01
Floor 3 Posted 2008-09-02 21:03 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
Thanks HAT. Using goto solved it, amazing.


Also, every day automatically compress all txt files generated on the previous day under d:\record
into another directory, stored as a compressed file named with the previous date. For example: d:\rardata\%date%-1.rar
How should that be done?

[ Last edited by qwertl on 2008-9-2 at 09:19 PM ]
Floor 4 Posted 2008-09-02 21:47 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
qwertl +2 2008-09-12 22:01
Floor 5 Posted 2008-09-02 21:59 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
This is workable, but the txt filenames are:Ter2008Y4M16D12h.txt
Ter2008Y4M16D13h.txt
Ter2008Y4M16D14h.txt
Ter2008Y4M16D15h.txt
: one such file is generated every hour.

If the txt gets one line of record every second, won't that also take quite a bit of CPU and memory resources? Is it possible that whenever a new txt file is generated, the txt files generated earlier in the meantime are automatically moved to another directory? That way it probably won't keep reprocessing lots of outdated values.
I already learned how to do the compression problem from the previous post, though I still need to study a lot of commands before I can understand them. Really thank you!

[ Last edited by qwertl on 2008-9-2 at 10:04 PM ]
Floor 6 Posted 2008-09-02 22:12 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Are these text files generated by a batch file? If so, just add a move in that batch file, right?
Floor 7 Posted 2008-09-02 22:18 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
The text files are not generated by a batch file, they are data generated in a fixed format by an rs232 device, so I can't control that part, but the number of records in each file can be set by making the time interval a bit longer,

for /f "tokens=*" %%a in ('dir /s /b /a-d /o-d"%SrcDir%"') do (
Can this specify that only the first line's txt is passed to %%a? If so, then it would be enough to process only the newest generated txt file.

If it can't specify only the first line, then is it possible to keep the first line's TXT file and move all the other TXT files to another directory first, and then execute the code in post #2.

[ Last edited by qwertl on 2008-9-2 at 10:41 PM ]
Floor 8 Posted 2008-09-02 22:55 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Because of the existence of the goto statement, shouldn't what follows only process the first line's txt, that is, only process the newest generated txt file
Floor 9 Posted 2008-09-02 23:01 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
Would this make it a little clearer:
@echo off
set SrcDir=C:\test
:begin
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%SrcDir%\*.txt"') do (
set NewFile=%%a
goto :next
)
goto :eof

:next
for /f "usebackq tokens=4 delims=. " %%b in ("%NewFile%") do (
if %%b gtr 30 (
echo start 另一个批处理程序.bat
ping -n 10 127.1 >nul
goto :begin
)
)
ping -n 10 127.1 >nul
goto :begin
Floor 10 Posted 2008-09-02 23:07 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
C:\>set SrcDir=C:\test

C:\>dir /b /a-d /o-d "C:\test\*.txt
TEMPer2008Y3M30D9h.txt
TEMPer2008Y4M16D15h.txt
TEMPer2008Y3M28D15h.txt

Running the code from post #9, it keeps looping with the message system cannot find the file TEMPer2008Y3M30D9h.txt.
Records in the file c:\test\TEMPer2008Y3M30D9h.txt:
1 2008-3-30 9:19:25 0
2 2008-3-30 9:19:26 0
3 2008-3-30 9:19:27 0
4 2008-3-30 9:19:28 0
5 2008-3-30 9:19:29 0
6 2008-3-30 9:19:30 31
7 2008-3-30 9:22:32 -13.25
8 2008-3-30 9:22:32 -13.25

____________________________________________________________

C:\>usbt

C:\>set SrcDir=C:\test

C:\>for /F "tokens=*" %a in ('dir /b /a-d /o-d "C:\test\*.txt"') do (
set NewFile=%a
goto :next
)

C:\>(
set NewFile=TEMPer2008Y3M30D9h.txt
goto :next
)

C:\>for /F "usebackq tokens=4 delims=. " %b in ("TEMPer2008Y3M30D9h.txt") do (if
%b GTR 30 (
echo start c:\12.bat
ping -n 10 127.1 1>nul
goto :begin
) )
The system cannot find the file TEMPer2008Y3M30D9h.txt.

C:\>ping -n 10 127.1 1>nul

C:\>goto :begin

C:\>for /F "tokens=*" %a in ('dir /b /a-d /o-d "C:\test\*.txt"') do (
set NewFile=%a
goto :next
)

C:\>(
set NewFile=TEMPer2008Y3M30D9h.txt
goto :next
)

C:\>for /F "usebackq tokens=4 delims=. " %b in ("TEMPer2008Y3M30D9h.txt") do (if
%b GTR 30 (
echo start c:\12.bat
ping -n 10 127.1 1>nul
goto :begin
) )
The system cannot find the file TEMPer2008Y3M30D9h.txt.

C:\>ping -n 10 127.1 1>nul
^CTerminate batch job (Y/N)?

[ Last edited by qwertl on 2008-9-2 at 11:46 PM ]
Floor 11 Posted 2008-09-02 23:53 ·  美国 惠普HP
版主
★★★★★
Credits 9,023
Posts 5,017
Joined 2007-05-31 19:39
19-year member
UID 89899
Gender Male
Status Offline
The second for should use an absolute path
@echo off
set SrcDir=C:\test
:begin
for /f "tokens=*" %%a in ('dir /b /a-d /o-d "%SrcDir%\*.txt"') do (
set NewFile=%%a
goto :next
)
goto :eof

for /f "usebackq tokens=4 delims=. " %%b in ("%SrcDir%\%NewFile%") do (
if %%b gtr 30 (
echo start 另一个批处理程序.bat
ping -n 10 127.1 >nul
goto :begin
)
)
ping -n 10 127.1 >nul
goto :begin
Floor 12 Posted 2008-09-03 00:29 ·  中国 广东 东莞 电信
中级用户
★★
Credits 461
Posts 243
Joined 2007-10-14 16:56
18-year member
UID 99730
Gender Male
Status Offline
It works now, awesome.
Floor 13 Posted 2008-09-03 04:37 ·  中国 广东 广州 电信
新手上路
Credits 18
Posts 9
Joined 2008-08-09 07:02
17-year member
UID 123015
Gender Male
Status Offline
So batch files can be used like this too, learned something, thanks
Forum Jump: