Results 1 to 3 of 3

Thread: Extracting strings from messy text files

Threaded View

  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

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