To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Part 10 of the Visual Basic .NET 2010 Express Tutorial Complete!
How to Use the Visual Studio Code Analysis Tool FxCop
Article :: Interview with Andrei Alexandrescu (Part 3 of 3)
Introducing Visual Studio LightSwitch
Visual Studio LightSwitch Beta 1 is Available



Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier

Reply Post New Thread
 
Thread Tools Display Modes
Old Apr 12th, 2005, 05:34 PM   #1
bat711
Addicted Member
 
Join Date: Mar 05
Location: Chicago
Posts: 136
bat711 is an unknown quantity at this point (<10)
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.     Dim Time2 As String
  6.    
  7.     inTaskMasterSection = False
  8.     TenCount = 0
  9.     Time = ""
  10.     TimeSum = ""
  11.     junk = 0
  12.     Time2 = "00:00"
  13.    
  14.     If InStr(1, strFileName, "(AS3)") Then
  15.         isAS3 = True
  16.     Else
  17.         isAS3 = False
  18.     End If
  19. Open strFileName For Input As #1
  20.     Do While Not EOF(1)
  21.         Line Input #1, strReadLine
  22.         If InStr(1, strReadLine, "Sent:") Then
  23.             pos1 = InStr(1, strReadLine, ",")
  24.             pos2 = Len(strReadLine)
  25.             strDate = Mid(strReadLine, pos1 + 1, pos2 - pos1)
  26.         End If
  27.        
  28.         pos1 = ""
  29.        
  30.         If InStr(1, strReadLine, "S ") Then
  31.             pos1 = InStr(1, strReadLine, "?")
  32.             pos1 = InStr(pos1 + 1, strReadLine, "?") + 1
  33.                        
  34.             Do While Mid(strReadLine, pos1, 1) = " "
  35.                 pos1 = pos1 + 1
  36.             Loop
  37.            
  38.             pos2 = InStr(1, strReadLine, "/")
  39.             Time = Mid(strReadLine, pos1, pos2 - pos1)
  40.             'MsgBox (Time)
  41.             'MsgBox (TimeSum)
  42.            
  43.             'Begin Time Sum
  44.             Dim Time1 As String
  45.            
  46.             Dim strParts1() As String
  47.             Dim strParts2() As String
  48.             Dim intSeconds As Integer
  49.            
  50.             Time1 = Time
  51.                        
  52.             strParts1 = Split(Time1, ":")
  53.             strParts2 = Split(Time2, ":")
  54.            
  55.             intSeconds = CInt(strParts1(1)) + CInt(strParts2(1))
  56.             If intSeconds > 59 Then
  57.                 Time2 = CInt(strParts1(0)) + CInt(strParts2(0)) _
  58.                 + (CInt(strParts1(1)) + CInt(strParts2(1))) \ 60 _
  59.                 & ":" & Format((CInt(strParts1(1)) + CInt(strParts2(1))) Mod 60, "00")
  60.             Else
  61.                 Time2 = CInt(strParts1(0)) + CInt(strParts2(0)) _
  62.                 & ":" & Format((CInt(strParts1(1)) + CInt(strParts2(1))), "00")
  63.             End If
  64.             'MsgBox (Time2)
  65.             'End Time Sum
  66.            
  67.         End If
  68.     Loop
  69.     Close #1
  70.    
  71.     TimeSum = Time2
Attached Files
File Type: txt logfile.txt (4.1 KB, 10 views)
bat711 is offline   Reply With Quote
Old Apr 12th, 2005, 05:48 PM   #2
visualAd
Swine Buddy
 
visualAd's Avatar
 
Join Date: Apr 02
Location: Langley, Berks, UK Mode: Restructuring
Posts: 4,835
visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)
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.
visualAd is offline   Reply With Quote
Old Apr 12th, 2005, 05:59 PM   #3
visualAd
Swine Buddy
 
visualAd's Avatar
 
Join Date: Apr 02
Location: Langley, Berks, UK Mode: Restructuring
Posts: 4,835
visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)visualAd is a splendid one to behold (700+)
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.
visualAd is offline   Reply With Quote
Reply

Go Back   VBForums > Visual Basic > Visual Basic 6 and Earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:24 AM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.