1 Attachment(s)
Extracting strings from messy text files
I'm trying to extract the time certain processes are running from log files similar to the attached one. Below is the code I'm using to extract the time in MM:SS format and then creating a running total of all the processes and importing it (code not included) into an access DB. Unfortunately it only works on about 30% of the files because of inconsistent formatting. Right now it is set up to identify the first '?' then the second '?' then find the '/' and extract what is between it (the time). The log file I've attached contains most (if not all) of the different layouts.
Any help greatly appreciated. :)
VB Code:
Dim strReadLine As String, inTaskMasterSection As Boolean
Dim pos1, pos2 As Integer, strDate As Date, strSQL As String
Dim TenCount As Integer, TimeSum As String, Time As String
Dim junk As Integer, isAS3 As Boolean
Dim Time2 As String
inTaskMasterSection = False
TenCount = 0
Time = ""
TimeSum = ""
junk = 0
Time2 = "00:00"
If InStr(1, strFileName, "(AS3)") Then
isAS3 = True
Else
isAS3 = False
End If
Open strFileName For Input As #1
Do While Not EOF(1)
Line Input #1, strReadLine
If InStr(1, strReadLine, "Sent:") Then
pos1 = InStr(1, strReadLine, ",")
pos2 = Len(strReadLine)
strDate = Mid(strReadLine, pos1 + 1, pos2 - pos1)
End If
pos1 = ""
If InStr(1, strReadLine, "S ") Then
pos1 = InStr(1, strReadLine, "?")
pos1 = InStr(pos1 + 1, strReadLine, "?") + 1
Do While Mid(strReadLine, pos1, 1) = " "
pos1 = pos1 + 1
Loop
pos2 = InStr(1, strReadLine, "/")
Time = Mid(strReadLine, pos1, pos2 - pos1)
'MsgBox (Time)
'MsgBox (TimeSum)
'Begin Time Sum
Dim Time1 As String
Dim strParts1() As String
Dim strParts2() As String
Dim intSeconds As Integer
Time1 = Time
strParts1 = Split(Time1, ":")
strParts2 = Split(Time2, ":")
intSeconds = CInt(strParts1(1)) + CInt(strParts2(1))
If intSeconds > 59 Then
Time2 = CInt(strParts1(0)) + CInt(strParts2(0)) _
+ (CInt(strParts1(1)) + CInt(strParts2(1))) \ 60 _
& ":" & Format((CInt(strParts1(1)) + CInt(strParts2(1))) Mod 60, "00")
Else
Time2 = CInt(strParts1(0)) + CInt(strParts2(0)) _
& ":" & Format((CInt(strParts1(1)) + CInt(strParts2(1))), "00")
End If
'MsgBox (Time2)
'End Time Sum
End If
Loop
Close #1
TimeSum = Time2
Re: Extracting strings from messy text files
The log file is consistant enough to be able to split the logfile line by the time field separator ":". It then just requires a couple of lines of code to extract the time:
VB Code:
Function GetTime(ByVal strLine As String) As String
Dim arr As Variant
Dim strRet As String
strRet = ""
arr = Split(strLine, ":")
' now extract the three fields hh:mm:ss - to return just mm:ss comment the first line
strRet = Right(arr(0), 2) & ":" 'hours
strRet = strRet & Left(arr(1), 2) & ":" 'mins
strRet = strRet & Left(arr(2), 2) 'secs
GetTime = strRet
End Function
Re: Extracting strings from messy text files
After seeing your other thread, I've realised you need the CPU time. Well the code is very similar to the one above:
VB Code:
Function GetCpuTime(ByVal strLine As String) As String
Dim arr As Variant
Dim strRet As String
strRet = ""
arr = Split(strLine, ":")
strRet = LTrim(Right(arr(2), 3)) & ":"
strRet = strRet & Left(arr(3), 2)
GetCpuTime = strRet
End Function