0

I want to get the last two line and compare their time, and judge whether the time difference more than 20 seconds. Below is My batch file script and test file. I have successfully got the last two line and print out. When I split the time and compare it, I found the time will leading zero. How could I comare the two time? thanks in advance!

My test file:

08:00:05 I 0-ChecK Loop = 0 Start
08:00:05 I 8124-ChecK Loop = 8124 Start
08:00:15 I 8125-ChecK Loop = 8125 Start
08:00:25 I 8126-ChecK Loop = 8126 Start
08:00:35 I 8127-ChecK Loop = 8127 Start
08:00:42 I 8128-ChecK Loop = 8128 Start

My batch file:

@echo off
setlocal enabledelayedexpansion

for %%A in (C:\Users\Howard\Desktop\test.txt) do (
    set "firstline="
    set "secondline="
    for /f "delims=" %%B in ('type "%%A"') do (
        set firstline=!secondline!
        set secondline=%%B
        )
    echo %%A !secondline! 
    )

for /f "tokens=1,2,3" %%i in ("!firstline!") do (
    set time1=%%i
    set I1=%%j
    set count1=%%k
)
echo %time1%
::set /A T1="%time1:~1,2% * 24"
::set /a T1=(%time1:~0,2% * 24) + (%time1:~3,2% * 60) + (%time1:~6,2% *60)
::echo %T1%
echo %I1%
echo %count1%

for /f "tokens=1,2,3" %%i in ("!secondline!") do (
    set time2=%%i
    set I2=%%j
    set count2=%%k
)
echo %time2%
echo %I2%
echo %count2%
pause

output:

enter image description here

Stephan
  • 53,940
  • 10
  • 58
  • 91
Howard
  • 143
  • 1
  • 1
  • 12
  • 3
    Convert your time string to seconds: `set /a secs1=(1%time1:~0,2%-100)*3600+(1%time1:~3,2%-100)*60+(1%time1:~6,2%-100)` – Stephan Jul 11 '21 at 06:40
  • Could you explain the 1 before %time1 and "-100" means what? thanks! – Howard Jul 11 '21 at 06:59
  • 4
    Numbers with leading zeros are handled as Octal. That's no problems with `00` to `07`, but `08` and `09` are invalid octal numbers, leading to errors. Therefore we put a `1` before, (converting `08` to `108`, substact `100` and finally get `8`. With that, we can happily do math. – Stephan Jul 11 '21 at 07:12
  • 2
    You could of course return the number of seconds by leveraging PowerShell in just one line, ```%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "$Content = Get-Content -LiteralPath '%UserProfile%\Desktop\test.txt' -Tail 2; (New-TimeSpan -Start ($Content[0] -Split ' ')[0] -End ($Content[1] -Split ' ')[0]).Seconds"``` – Compo Jul 11 '21 at 13:27
  • See [Arithmetic operations with HH:MM:SS times in batch file](https://stackoverflow.com/a/42603985/778560) – Aacini Jul 13 '21 at 00:06

1 Answers1

0

Here's a small extension of my commented example which should simply report whether the timespan was over twenty seconds:

@%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -NoProfile -Command "$Content = Get-Content -LiteralPath '%UserProfile%\Desktop\test.txt' -Tail 2; If ((New-TimeSpan -Start ($Content[0] -Split ' ')[0] -End ($Content[1] -Split ' ')[0]).TotalSeconds -GT 20) {Exit 2}"
@If ErrorLevel 2 Echo Timespan is over twenty seconds.
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Note that loading PowerShell needs time. This is not a problem for a one-time task (like here), but used in a tight loop, it has a big (bad) influence on execution time. – Stephan Jul 11 '21 at 14:30
  • I doubt it would ever be an issue @Stephan, first of all I'm starting `powershell.exe` without loading a profile. Then for the actual task, I'm using `-Tail 2`, which will be infinitely quicker than enumerating all of the lines in the file, saving each to a variable, overwriting the last until there's only two variables remaining. I do not have a Windows machine available at the moment, but I'd be really interested in timings for this vs the OP's three `for` loops method, with your suggested improved `set /a` integrated, _(and especially on a text file with several hundred lines in it)_. – Compo Jul 11 '21 at 15:48
  • well, I said `This is not a problem for a one-time task (like here)` (and yes, I too expect it to be faster (didn't test) for large and even medium files). Just saying it will be slower *in a loop* (for example converting *each line* (which isn't needed nor done here)) – Stephan Jul 12 '21 at 09:43