Do command when file 3rd-to-last line does NOT contains a string  


  1. Posts : 28
    Windows 10
       #1

    Do command when file 3rd-to-last line does NOT contains a string


    I'm having connection issues which I'm documenting. I have a close variant of the following that has 60 seconds in between each call, but I'd like one to be continuous until the connection is reestablished. The way I see it, at the end of the following there would be some way of checking the log content and if what's at the end of the log reflect a disconnection and if so immediately make another call.
    echo %time:~0,5%
    @set yymmdd=%date:~12,2%%date:~7,2%%date:~4,2%
    echo %date% >> logs\pinger-%yymmdd%.txt
    ::using "time /t | tee -a [...]" take 4 time the processing of doing the command twice
    echo %time:~0,5% >> logs\pinger-%yymmdd%.txt
    ping google.ca >> logs\pinger-%yymmdd%.txt
    pinger-win10.bat
    and since a good ping should end with something like
    Ping statistics for 172.217.13.99:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 12ms, Maximum = 12ms, Average = 12ms
    but when the ping goes bad it's like
    Request timed out.

    Ping statistics for 172.217.13.99:
    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),
    , I think the batch last line would be something like if
    SEARCH logs\pinger-%yymmdd%.txt -line m -match false "(0% loss)" then pinger-win10.bat REM else it stops
    ; that's just an example and I'm not good at these kind of things.

    TLDR How to do a command if a file 3rd-to-last line does NOT contains a specific string?

    Thank you kindly for your assistance
      My Computers


  2. Posts : 16,191
    Windows 10 Home x64 Version 22H2 Build 19045.3693
       #2

    You could use a For command that reads the log file by calling a subroutine.
    Each iteration of the subroutine could progressively read the current line and assign it to a variable CurrentLine after having, for line 3 onwards,
    - reassigned the last CurrentLine to another variable PreviousLine
    - reassigned PreviousLine to another variable PrePreviousLine
    When the For command iterations are complete, PrePreviousLine will contain the 3rd to last line. So you can then test for its contents
    - which would be Packets: Sent or Request timed out. since For commands do not process blank lines.

    What is your objective?
    - Making a decision based on whether or not you have an internet connection?
    - That can be done more simply [and without using the ping command which some public networks reject; my local library is the only example I can think of off the top of my head].
    - I have a batch file & a PowerShell version of this test.

    Denis
      My Computer


  3. Posts : 28
    Windows 10
    Thread Starter
       #3

    The OP was long so I understand you might have missed it, but I already have a minute logger, which will contain many thousand lines per day. Now I'd like something to be run continuously until the connection is back either manually when I notice it's disconnected, or preferably but not necessarily, automatically when there's a disconnection. The most important thing is it stops by itself; so I can go do something else offline and not worry about the log being huge for no reason (since that can be > 1 hr and a continuous ping log for that time would definitively be huge).

    I assume the continuous ping log will be a few hundred lines per day. But even if it never went beyond 200 lines, I think beyond 50 lines that the delay for a script as you suggested would add, say > 5 seconds, for something that's meant to be quick as I want to ping often, say every 10-15 seconds, when disconnected.
      My Computers


  4. Posts : 16,191
    Windows 10 Home x64 Version 22H2 Build 19045.3693
       #4

    Reading each line of very long log files would be a burden on your computer. If you separated the logs for each ping [using > instead of >>] then it would be easier for the computer to cope with even though the approach would be the same.

    Try3 said:
    What is your objective?

    Denis
      My Computer


  5. Posts : 28
    Windows 10
    Thread Starter
       #5

    Was repeating
    Try3 said:
    What is your objective?
    a mistake? If not my previous post 1st paragraph is my answer.

    I'd rather use the program tail through Cygwin than do it on separate files. I'd prefer to keep it Windows-native only but I install Cygwin on almost all the systems I spent a good amount of time on which I have sufficient privilege, so it's not a big problem if I have to.
      My Computers


  6. Posts : 453
    Windows 10
       #6

    Instead of continiously writing time and ping to the log why not make it like this:

    12:50 - 15:16 Internet OK
    15:16 - 17:04 Internet Interrupted
    17:04 - 20:05 Internet Ok
    20:05 - 21:30 Internet Interrupted

    You only log when the internet disconnects and only log when the internet is back...

    You can accomplish this by monitoring the errorlevel of the ping command...
      My Computer


  7. Posts : 16,191
    Windows 10 Home x64 Version 22H2 Build 19045.3693
       #7

    It was not a mistake. I do not know what your objective is.
    - All I know is how you intend to approach the task.
    - If I knew what the objective was I might be able to suggest an alternative approach.

    There is no alternative in either batch files or PowerShell to reading every line of a text file of indeterminate length in order to use a given line from the end to make a decision. So your approach seems, to me, to be doomed to being extremely slow & clunky.
    Batch files - For command - For - Simple file reading examples - TenForums
    PowerShell - Get-Content - MSDocs and Read file line by line in PowerShell - Stack Overflow

    I once played around with monitoring for network connections & disconnections automatically using wevutil [& possibly also Wecutil] but I could never produce a dependable solution.


    Denis
      My Computer


  8. Posts : 28
    Windows 10
    Thread Starter
       #8

    ricardobohner said:
    monitoring the errorlevel of the ping command
    1 ping / min is plenty for the overall goal. I'd just like to be more precise only when there's a disconnection which is almost always < 5 mins, but currently happen often.

    Try3 said:
    It was not a mistake. I do not know what your objective is.
    - All I know is how you intend to approach the task.
    - If I knew what the objective was I might be able to suggest an alternative approach.
    Where in the post #3 1st paragraph is there anything about approaching the task? I want something that ping like every 10 seconds but only until it reconnects; it can start manually or automatically but it need to stop automatically. I don't think I can be any cleared than that.
      My Computers


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 04:31.
Find Us




Windows 10 Forums