Q:
Hello, script expert! How to calculate the number of times that the word "Failure" appears in the log file? There is a problem: Because the log file writes events one by one, a very long text line is created.
-- FS
A:
Hello, FS. According to the rest of your email, 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 PM Failure 2/8/2006 AM Success 2/7/2006 PM
As you mentioned, the first thing you think of is to use the InStr function to check whether Failure appears somewhere in the log file. Then you can use the activity counter to calculate the number of times that the word is found, this is very similar to the method we demonstrated in yesterday's column. This is a good idea, but, as you have discovered, there is a major defect in this solution: Technically, your log file contains only one line. Therefore, despite the fact that many instances exist, your script always reports only one Failure instance. You wrote in the email: But I am in a dilemma because I cannot split a single row into multiple rows.
Alas, you have no confidence in us. Try this to see if it is appropriate:
Copy codeThe Code is 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 to solve this problem. We use this method because it fully fits your original ideas and we think it is very easy for everyone to understand. We mentioned this only to prevent people from thinking, "Hey, I won't solve this problem ." No problem: this is not the only answer. It is just one of the answers.
Okay. What about the script itself? Well, we first define a constant named ForReading. We will use this constant when opening the log file later. Next we will create a Scripting. FileSystemObject instance and use the OpenTextFile method to open the file C: \ Scripts \ Test. log. After the file is opened, we use the ReadAll method to read all the content of the file to 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. We use I as the active counter to calculate each instance of the word "Failure. Then we use the following line of code:
ArrLines = Split (strContents ,"")
Remember what you said, you are difficult, because the entire log file is a very long line? Well, here we mainly split your log file (or the version stored in the variable strContents at least) into a group of shorter lines. A single word is separated by space in your log file. In this line of code, we use the Split function to Split the strContents value into arrays. By splitting spaces (that is, each time a space is encountered, a new item is created in the array) we get an array starting 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 create a For Each loop to traverse Each item in the array; more importantly, we can also use the InStr method to check whether the word "Failure" can be found in any row of these rows. If possible, we will increase the counter variable I value. All of these are executed by the following code:
For Each strLine in arrLines
If InStr (strLine, "Failure") Then
I = I + 1
End If
Next
After we traverse the For Each loop, all we need to do is to echo the failure and then complete it.
Now, check: All we have to do is display the number of failures found in the log file. Your script may have timed out a long time before returning all the failures. (But we still think that writing scripts with celebrities is more interesting than watching Dancing with celebrities or skating .)