Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Capturing text on a line

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,31426
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Capturing text on a line
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Capturing text on a line - 2/22/2006 6:15:23 AM   
  Joiry

 

Posts: 14
Score: 0
Joined: 2/22/2006
Status: offline
All,
I hope I have this in the right forum.  I’m new to the forum and have been scripting for about 12 hours so that is completely new too.  I’m hoping to gain a little help on a script I’m writing to scan a log file for errors.  I have the log file opened and can read it line by line with out issue.  I’m using instr to find the word ERROR which is what I’m looking for line by line.  That’s all working great.
My quandary is the next step.  I would like to capture the error number.  Unfortunately this isn’t a finite set of digits for me to capture after the word ERROR.  My error code can be 2 characters or up to 4.  And there is text after the code which further messes me up. 
Two examples of my log file:
2006/02/21 00:51:35 ERROR 33 (0x00000021) Copying File G:\SHAR …
2006/02/21 00:52:59 ERROR 999 (0x000003E7) Copying …
What I want to capture is the number (string is fine) after the word ERROR.  I’ve tried working out some kind of kludge loop trying to go character by character after the word ERROR and looking for a space, capturing that is between the spaces and parsing it together.  I’m not having too much success in getting that to work, and furthermore it seems highly inefficient.  I’m really hoping there is a better way.
This is my function of how I’m reading the file:  I figure it’s in this “if” statement I need to put logic to capture the next “word” after error.
  Do Until objLogFile.AtEndOfStream
     strLine = objLogFile.ReadLine
     if instr(strLine, "ERROR") > 0
Anybody got something simple?
Thanks,
Joiry
 
 
Post #: 1
 
 RE: Capturing text on a line - 2/22/2006 6:35:47 AM   
  ziminski

 

Posts: 79
Score: 2
Joined: 1/8/2006
Status: offline
If the log format is always the way you're example is , look at using the split function.

(in reply to Joiry)
 
 
Post #: 2
 
 RE: Capturing text on a line - 2/22/2006 6:48:48 AM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Or if you want to use regexes, this pattern should work:

"ERROR (\d{2,4})"

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to ziminski)
 
 
Post #: 3
 
 RE: Capturing text on a line - 2/22/2006 9:07:41 PM   
  ginolard


Posts: 1082
Score: 21
Joined: 8/10/2005
Status: offline
I see a lot of these sorts of questions and it's just occurred to me that Log Parser from Microsoft might be a good option.  I admit I've not used it much but it seems ideal for this sort of thing.

_____________________________

Author of ManagePC - http://managepc.net
AD Query Template - http://www.visualbasicscript.com/m_40609/tm.htm
Consolidated Scripting Framework - http://www.visualbasicscript.com/m_59109/tm.htm

(in reply to ebgreen)
 
 
Post #: 4
 
 RE: Capturing text on a line - 2/23/2006 12:37:59 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
MyString = "2006/02/21 00:51:35 ERROR 33 (0x00000021) Copying File G:\SHAR "
ErrorNumber = Mid(MyString,  InStr(MyString,"ERROR")+6 ,  InStr(MyString,"(" ) - (InStr(MyString,"ERROR")+6)  )

_____________________________

Fred

(in reply to ginolard)
 
 
Post #: 5
 
 RE: Capturing text on a line - 2/23/2006 12:41:31 AM   
  Joiry

 

Posts: 14
Score: 0
Joined: 2/22/2006
Status: offline
Hi,
I wanted to express thanks for the help and let everybody know I got it worked out.  I have no idea what regexes are, but the split function worked well for me.  Strangely, the function isn’t referenced at all in the guide I’m using.  But based upon this string I found some syntax for it on Microsoft’s site.
If anybody else has this issue, below is the logic I used.  It’s likely not the cleanest code, but it’s working for me.
Thanks again.
 
  Do Until objLogFile.AtEndOfStream
     strLine = objLogFile.ReadLine
     intErrorCounter = instr(strLine, "ERROR")
     if intErrorCounter > 0 then
       tempText = split(mid(strLine, intErrorCounter, 20), " ")
'       message = message + "Error " & tempText(1) & " found on line " & intLineNum & chr(13)
       select case ucase(tempText(1))                                  
         case "123" : message = message + "Long path name error on line " & intLineNum & chr(13)
         case "5" : message = message + "Access Denied error on line " & intLineNum & chr(13)
         case "31" : message = message + "Likely corrupt file on line " & intLineNum & chr(13)
       end select
       intErrorCounter = intErrorCounter + 1
     end if
     intLineNum = intLineNum + 1
     tempText = tempText2                'Easiest way I can find to empty an array!
  Loop

(in reply to Joiry)
 
 
Post #: 6
 
 RE: Capturing text on a line - 2/23/2006 11:27:46 AM   
  Fredledingue


Posts: 383
Score: 0
Joined: 5/9/2005
From:
Status: offline
Yes, splitting a string is sometimes easier to figure out than the InStr and Mid that I used.

Note:

tempText = split(mid(strLine, intErrorCounter, 20), " ")  
'       message = message + "Error " & tempText(1) & " found on line " & intLineNum & chr(13)
 
can be rewritten

1/  message = message + "Error " &   split(mid(strLine, intErrorCounter, 20), " ") (1)  & " found on line " & intLineNum & VbCrlf 
If you don't need to keep the array for other things.

2/ You can write VbCrlf instead of chr(13)



_____________________________

Fred

(in reply to Joiry)
 
 
Post #: 7
 
 RE: Capturing text on a line - 2/23/2006 12:09:28 PM   
  ebgreen


Posts: 5246
Score: 31
Joined: 7/12/2005
Status: offline
Not to quibble, but vbCrLf is equivalent to Chr(13) & Chr(10).

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to Fredledingue)
 
 
Post #: 8
 
 RE: Capturing text on a line - 2/23/2006 12:56:06 PM   
  Joiry

 

Posts: 14
Score: 0
Joined: 2/22/2006
Status: offline
It's not quibbling, I just didn't know.  Thanks for the tip.

I also converted my function to Fred's way this afternoon.  I found it to be a little cleaner and liked it a lot.  It's always good to know multiple ways to handle a problem.

Thanks again.

(in reply to Joiry)
 
 
Post #: 9
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Capturing text on a line Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts