Jump to content

Look for a time in a log file


Go to solution Solved by Melba23,

Recommended Posts

Hey fellas,

I have this log file that looks like this:

[18:45:12]: Stuff1
[18:45:23]: Stuff22
[18:45:23]: Stuff98
[19:02:37]: Stuff7
[19:02:38]: Stuff1
[19:02:38]: Stuff1324
...

What I need is a script that would periodically check the time in the log and if the difference between the last log entry and the current time is greater than X it would do Y.

Is that something that could be done?

Thanks a lot

Link to comment
Share on other sites

  • Moderators
  • Solution

Seminko,

Not too difficult. Read the file into a variable (an array is probably best), extract the most recent time (either first or last entry) and get the difference from the current time. Functions you might need are:

FileReadToArray

_StringBetween

_DateDiff

_NowCalc

Give it a go and see how you get on - you know where we are if you run into problems. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Seminko,

Not too difficult. Read the file into a variable (an array is probably best), extract the most recent time (either first or last entry) and get the difference from the current time. Functions you might need are:

FileReadToArray

_StringBetween

_DateDiff

_NowCalc

Give it a go and see how you get on - you know where we are if you run into problems. ;)

M23

Hello Melba, nice seeing you again :).

Is there a way for the script to recognize the last udpated file in a folder?

For some reason this does not work:

While 1
   Local $LastLine = FileReadLine ("Log-2014-08-26 18-45-12.txt", -1)
   Local $aArray = _StringBetween($LastLine, "[", "]")
   MsgBox(1, "aaa", $aArray)
   Sleep(5000)
WEnd

It returns nothing. When I let MsgBox show me $LastLine, it shows me the correct stuff: [19:31:17]: Stuff

Alright - $aArray[0]

Edited by Seminko
Link to comment
Share on other sites

Hi. look FileGetTime

 

Look this.

#include <Array.au3>
#include <String.au3>



Local $aArray = FileReadToArray("yourfilehere")

For $i = 0 To UBound($aArray) - 1
    $aArray[$i] = _StringBetween($aArray[$i], "[", "]")[0]
Next

_ArrayDisplay($aArray)
 

Saludos

Edited by Danyfirex
Link to comment
Share on other sites

Here is a short example on how to get your times compatible with _dateDiff() which gets the difference between dates or times.

$aFile = FileReadToArray("MyLog.txt")

For $i = 0 To UBound($aFile) - 1
    ConsoleWrite(_Time_To_Date_Time($aFile[$i]) & @LF)
Next

Func _Time_To_Date_Time($Time)
    Return StringLeft(_NowCalc(), 10) & " " & _StringBetween($Time, "[", "]")[0]
EndFunc

EDIT: Or this might be easier to understand.

$aTimes = _StringBetween(FileRead("MyLog.txt"), "[", "]")

For $i = 0 To UBound($aTimes) - 1
    ConsoleWrite(_Time_To_Date_Time($aTimes[$i]) & @LF)
Next

Func _Time_To_Date_Time($Time)
    Return StringLeft(_NowCalc(), 10) & " " & $Time
EndFunc
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

 

Here is a short example on how to get your times compatible with _dateDiff() which gets the difference between dates or times.

$aFile = FileReadToArray("MyLog.txt")

For $i = 0 To UBound($aFile) - 1
    ConsoleWrite(_Time_To_Date_Time($aFile[$i]) & @LF)
Next

Func _Time_To_Date_Time($Time)
    Return StringLeft(_NowCalc(), 10) & " " & _StringBetween($Time, "[", "]")[0]
EndFunc

EDIT: Or this might be easier to understand.

$aTimes = _StringBetween(FileRead("MyLog.txt"), "[", "]")

For $i = 0 To UBound($aTimes) - 1
    ConsoleWrite(_Time_To_Date_Time($aTimes[$i]) & @LF)
Next

Func _Time_To_Date_Time($Time)
    Return StringLeft(_NowCalc(), 10) & " " & $Time
EndFunc

Well done, thx

Link to comment
Share on other sites

Right, got the first part. Now I have to make the script open the correct file.

Hi. look FileGetTime

 

Look this.

#include <Array.au3>
#include <String.au3>



Local $aArray = FileReadToArray("yourfilehere")

For $i = 0 To UBound($aArray) - 1
    $aArray[$i] = _StringBetween($aArray[$i], "[", "]")[0]
Next

_ArrayDisplay($aArray)
 

Saludos

Could you please provide and example of looking through a folder to identify the file that was modified last? 

Link to comment
Share on other sites

Google tested it for me ;)

$last = 0
$sFolder = "F:\Stuff\Logs\"
If @Error = 1 Then exit
$aFiles = _FileListToArray($sFolder , "*" , 1)

for $i = 1 to $aFiles[0]
$time = filegettime($sFolder & "\" & $aFiles[$i] , 0 , 1)
If $time > $last Then
$newfile = $aFiles[$I]
$last = $time
EndIf
next

msgbox(0, '' , $newfile)

God damn I still don't really understand loops ie for $i do bla bla... :-/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...