Results 1 to 3 of 3

Thread: Extracting strings from messy text files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    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:
    1. Dim strReadLine As String, inTaskMasterSection As Boolean
    2.     Dim pos1, pos2 As Integer, strDate As Date, strSQL As String
    3.     Dim TenCount As Integer, TimeSum As String, Time As String
    4.     Dim junk As Integer, isAS3 As Boolean
    5.  
    6.     Dim Time2 As String
    7.    
    8.     inTaskMasterSection = False
    9.     TenCount = 0
    10.     Time = ""
    11.     TimeSum = ""
    12.     junk = 0
    13.     Time2 = "00:00"
    14.    
    15.     If InStr(1, strFileName, "(AS3)") Then
    16.         isAS3 = True
    17.     Else
    18.         isAS3 = False
    19.     End If
    20.  
    21. Open strFileName For Input As #1
    22.     Do While Not EOF(1)
    23.         Line Input #1, strReadLine
    24.  
    25.         If InStr(1, strReadLine, "Sent:") Then
    26.             pos1 = InStr(1, strReadLine, ",")
    27.             pos2 = Len(strReadLine)
    28.             strDate = Mid(strReadLine, pos1 + 1, pos2 - pos1)
    29.         End If
    30.        
    31.         pos1 = ""
    32.        
    33.         If InStr(1, strReadLine, "S ") Then
    34.             pos1 = InStr(1, strReadLine, "?")
    35.             pos1 = InStr(pos1 + 1, strReadLine, "?") + 1
    36.                        
    37.             Do While Mid(strReadLine, pos1, 1) = " "
    38.                 pos1 = pos1 + 1
    39.             Loop
    40.            
    41.             pos2 = InStr(1, strReadLine, "/")
    42.             Time = Mid(strReadLine, pos1, pos2 - pos1)
    43.             'MsgBox (Time)
    44.             'MsgBox (TimeSum)
    45.            
    46.             'Begin Time Sum
    47.             Dim Time1 As String
    48.            
    49.             Dim strParts1() As String
    50.             Dim strParts2() As String
    51.             Dim intSeconds As Integer
    52.            
    53.             Time1 = Time
    54.                        
    55.             strParts1 = Split(Time1, ":")
    56.             strParts2 = Split(Time2, ":")
    57.            
    58.             intSeconds = CInt(strParts1(1)) + CInt(strParts2(1))
    59.             If intSeconds > 59 Then
    60.                 Time2 = CInt(strParts1(0)) + CInt(strParts2(0)) _
    61.                 + (CInt(strParts1(1)) + CInt(strParts2(1))) \ 60 _
    62.                 & ":" & Format((CInt(strParts1(1)) + CInt(strParts2(1))) Mod 60, "00")
    63.             Else
    64.                 Time2 = CInt(strParts1(0)) + CInt(strParts2(0)) _
    65.                 & ":" & Format((CInt(strParts1(1)) + CInt(strParts2(1))), "00")
    66.             End If
    67.             'MsgBox (Time2)
    68.             'End Time Sum
    69.            
    70.         End If
    71.     Loop
    72.  
    73.     Close #1
    74.    
    75.     TimeSum = Time2
    Attached Files Attached Files

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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:
    1. Function GetTime(ByVal strLine As String) As String
    2.     Dim arr As Variant
    3.     Dim strRet As String
    4.    
    5.     strRet = ""
    6.     arr = Split(strLine, ":")
    7.    
    8.     ' now extract the three fields hh:mm:ss - to return just mm:ss comment the first line
    9.     strRet = Right(arr(0), 2) & ":" 'hours
    10.     strRet = strRet & Left(arr(1), 2) & ":"  'mins
    11.     strRet = strRet & Left(arr(2), 2) 'secs
    12.    
    13.     GetTime = strRet
    14. End Function
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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:
    1. Function GetCpuTime(ByVal strLine As String) As String
    2.     Dim arr As Variant
    3.     Dim strRet As String
    4.    
    5.     strRet = ""
    6.     arr = Split(strLine, ":")
    7.    
    8.     strRet = LTrim(Right(arr(2), 3)) & ":"
    9.     strRet = strRet & Left(arr(3), 2)
    10.    
    11.     GetCpuTime = strRet
    12. End Function
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width