批处理文件计时器和持续时间计算器

批处理文件计时器和持续时间计算器

我制作了这个批处理文件,它可用作两个事件的计时器,并无限期地循环执行它们(直到您将其终止)。它还计算第一个事件所花费的时间加上第二个事件开始前所经过的时间。它将输出发布到文本文件中。计时器工作正常,但持续时间计算有时不太准确(但大多数情况下工作正常)。

问题:

  1. 如果计时器在凌晨 2 点(可能是凌晨 12 点,我当时还没有测试过)到上午 9:59 之间启动,则不会计算持续时间。相反,我会将第一个事件的最后停止时间发布到文本文件中两次。

  2. 有时,当我在早上运行计时器时,尤其是早上 5 点或 6 点左右,即使有开始和停止时间,持续时间也会计算为 00:00:00。有趣的是,计时器大多数时候都运行正常,并且只会随机产生此问题。

  3. 是否可以跳过一个事件的计时器并继续下一个事件而不终止批处理作业? 如果是,怎么做? 请发布示例代码。

有人能指出代码错误的地方并发布修复方法吗?拜托了。

如果有人因为认为此帖子不重要、没用等而想关闭此帖子,请先解决我的问题或让别人解决,然后再关闭帖子。

在尝试在这里发帖之前,我花了大量时间尝试解决这些问题,所以请您理解。

以下是代码:

@ECHO off

MODE CON:COLS=46 LINES=9

echo --------------------------------- >> C:\Users\username\Desktop\Progress.txt

echo. >> C:\Users\username\Desktop\Progress.txt

echo %date% >> C:\Users\username\Desktop\Progress.txt

set counter=0

set TST=0

:1

@echo off

setlocal

rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%

@echo off 

set /a counter=counter+1

set /a TST=TST+%duration%

rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A TSTH=%TST% / 360000
set /A TSTM=(%TST% - %TSTH%*360000) / 6000
set /A TSTS=(%TST% - %TSTH%*360000 - %TSTM%*6000) / 100
set /A TSTHS=(%TST% - %TSTH%*360000 - %TSTM%*6000 - %TSTS%*100)

rem some formatting
if %TSTH% LSS 10 set TSTH=0%TSTH%
if %TSTM% LSS 10 set TSTM=0%TSTM%
if %TSTS% LSS 10 set TSTS=0%TSTS%
if %TSTHS% LSS 10 set TSTHS=0%TSTHS%

echo. >> C:\Users\username\Desktop\Progress.txt

echo Total Time: %TSTH%:%TSTM%:%TSTS% Plus >> C:\Users\username\Desktop\Progress.txt
echo Started on: %time%  >> C:\Users\username\Desktop\Progress.txt


color f0

cls 

:hour

set hour=1                            

:min

set min=0

:second

set second=0

:countit

cls                                                          

echo                  Main Event
echo            %date%
echo.
echo   %min%    Minutes  
echo   %sec%    Seconds
echo        Hours #    %counter%
echo        Total Time: %TSTH%:%TSTM%:%TSTS%

set /a sec="%sec%-1"

if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto 2

ping -n 2 127.0.0.1 >NUL

goto countit

:2

start "" "C:\Users\username\Desktop\Timer file\1.mp3"

echo.

pause>nul

echo Are You Sure?

pause>nul

cls

echo Okay then, Ready..

Pause>nul

@echo off

echo stopped on: %time%  >> C:\Users\username\Desktop\Progress.txt

set ENDTIME=%TIME%

rem output as time
echo STARTTIME: %STARTTIME%
echo ENDTIME:    %ENDTIME%

rem convert STARTTIME and ENDTIME to centiseconds
set /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)

set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)

rem calculating the duratyion is easy
set /A DURATION=%ENDTIME%-%STARTTIME%

rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A DURATIONH=%DURATION% / 360000
set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)

rem some formatting
if %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%
if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%
if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%
if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%

rem outputing
echo Duration is %DURATIONH%:%DURATIONM%:%DURATIONS% >> C:\Users\username\Desktop\Progress.txt

cls

:4

color e0

cls

:hour

set hour=0                           

:min

set min=10

:sec

set sec=0

:continue

cls                                                             

echo                   Break...
echo            %date%
echo.                                                           
echo   %min%    Minutes  
echo   %sec%    Seconds
echo        Hours #    %counter%
echo        Total Time: %TSTH%:%TSTM%:%TSTS%

set /a sec="%sec%-1"

if %sec%==-1 set /a min="%min%-1"
if %sec%==-1 set /a sec="59"
if %min%==-1 set /a hour="%hour%-1"
if %min%==-1 set /a min="59"
if %hour%==-1 goto 3

ping -n 2 127.0.0.1 >NUL

goto continue

:3

start "" "C:\Users\username\Desktop\Timer file\2.mp3"

echo.

pause>nul

echo Are You Sure?

pause>nul

cls

echo Okay then, Ready..

Pause>nul

cls

goto 1

相关内容