Use VBS to calculate the number of occurrences of a word in a log file _vbs

Source: Internet
Author: User
Tags constant
Ask:
Hello, Scripting Guy! How do I calculate the number of occurrences of the word failure in a log file? Here's the problem: because the log files are written one after another, a very long line of text is created.
--FS
For:
Hello, FS. Depending on the rest of your e-mail, your log file is similar to the following:
Failure 2/7/2006 8:25 AM failure 2/7/2006 9:45 AM Success
2/7/2006 3:10 pm Failure 2/8/2006 9:15 AM Success 2/7/2006 pm
As you mentioned, the first thing you think of is using the INSTR function to see if failure appears somewhere in the log file, and then you can compute the number of times to find the word through the activity counter, which is very similar to the method we demonstrated in yesterday's column. That's a good idea, but, as you've found, there's a major flaw in the scenario: Technically, your log file contains only one row. Therefore, despite the fact that there are many instances, your script always reports that only one instance of failure is found. You wrote in the Mail: But I was stumped because I couldn't split a single line into multiple lines.
Alas, you have no confidence in us at all. Try this to see if it's appropriate:
Copy Code code as follows:

Const ForReading = 1
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set objfile = objFSO.OpenTextFile ("C:\scripts\test.log", ForReading)
StrContents = Objfile.readall
Objfile.close
i = 0
Arrlines = Split (strContents, "")
For each strLine in Arrlines
If InStr (StrLine, "failure") Then
i = i + 1
End If
Next
WScript.Echo "Number of failures:" & I
Of course, there are other ways we can solve this problem. We take this approach because it fits your original idea perfectly, and we think it's very easy for everyone to understand. We mentioned this only to prevent people from reading this and thinking, "Hey, I'm not going to solve this problem." "No problem: It's not the only answer." It's just one of the answers.
Well, what about the script itself? Well, we first define a constant named ForReading, and we'll use this constant when we open the log file later. Next we create an instance of Scripting.FileSystemObject and use the OpenTextFile method to open the file C:\Scripts\Test.log. Once the file is open, we use the ReadAll method to read the entire contents of the file into the variable strContents, and then close the Test.log file.
Are you clear? Next we assign the value 0 to the counter variable named I, and we will use I as the activity counter to compute each instance of the word we encounter failure. Then we use the following line of code:
Arrlines = Split (strContents, "")
Remember you said that you were stumped because the log file is a very long line? Well, we're here to divide your log file (or at least the version stored in the variable strContents) into a shorter set of rows. In your log file, individual words are separated by spaces. In this line of code, we use the Split function to "split" the StrContents value into an array, by splitting the space (that is, by creating a new item in the array each time a space is encountered), we get a list that starts with the following:
Failure
2/7/2006
8:25
Am
Failure
2/7/2006
9:45
Am
Success
Of course, it looks funny, but now we can set up a For Each loop to walk through each item in the array, and more importantly, we can also use the InStr method to see if the word failure can be found in any row of those rows. If we can, we will increase the value of the counter variable i. All of these are performed by the following code:
For each strLine in Arrlines
If InStr (StrLine, "failure") Then
i = i + 1
End If
Next
After we iterate through the For Each loop, all we have to do is echo failure, and then we're done.
Now, check: all we have to do is echo back the number of failure found in the log file. Your script may have timed out a long time before completing all failure to echo. (But we still think it's more fun to write a script with a celebrity than to see a celebrity dance or skate.) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.