{"id":67993,"date":"2006-02-09T11:10:00","date_gmt":"2006-02-09T11:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/09\/how-can-i-count-the-number-of-times-a-word-appears-in-a-log-file\/"},"modified":"2006-02-09T11:10:00","modified_gmt":"2006-02-09T11:10:00","slug":"how-can-i-count-the-number-of-times-a-word-appears-in-a-log-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-count-the-number-of-times-a-word-appears-in-a-log-file\/","title":{"rendered":"How Can I Count the Number of Times a Word Appears in a Log File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I count the number of times the word <I>Failure<\/I> appears in a log file? There\u2019s one catch, though: the log file simply writes its events one after another, creating one big, giant line of text.<BR><BR>&#8212; FS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, FS. According to the rest of your email, your log file looks something like this:<\/P><PRE class=\"codeSample\">Failure 2\/7\/2006 8:25 AM Failure 2\/7\/2006 9:45 AM Success \n2\/7\/2006 3:10 PM Failure 2\/8\/2006 9:15 AM Success 2\/7\/2006 3:01 PM\n<\/PRE>\n<P>As you also noted, your first thought was to use the <B>InStr<\/B> function to see if the word <I>Failure<\/I> appears anywhere in each line of the log file; you could then keep a running tally of the number of times you found the word, a technique remarkably similar to the one we demonstrated in <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/feb06\/hey0208.mspx\"><B>yesterday\u2019s column<\/B><\/A>. That was a good idea, but, as you discovered, there was one major flaw in the plan: technically your log file has only one line in it. Consequently, your script always reported that it found one instance of the word <I>Failure<\/I>, regardless of how many instances actually existed. \u201cBut I\u2019m stuck,\u201d you wrote, \u201cbecause there\u2019s no way to break this single line into multiple lines.\u201d<\/P>\n<P>Oh ye of little faith. Try this on for size:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;c:\\scripts\\test.log&#8221;, ForReading)\nstrContents = objFile.ReadAll\nobjFile.Close<\/p>\n<p>i = 0<\/p>\n<p>arrLines = Split(strContents, &#8221; &#8220;)<\/p>\n<p>For Each strLine in arrLines\n    If InStr(strLine, &#8220;Failure&#8221;) Then\n        i = i + 1\n    End If\nNext<\/p>\n<p>Wscript.Echo &#8220;Number of failures: &#8221; &amp; i\n<\/PRE>\n<P>Now, admittedly, there are a couple other ways we could have solved this problem. We went with this approach because it piggybacked nicely on your original thought, and because we thought it would be fairly easy for everyone to understand. We mention this simply in case anyone is reading this and thinking, \u201cMan, that\u2019s not how <I>I<\/I> would have solved this.\u201d That\u2019s fine: this isn\u2019t <I>the<\/I> answer. It\u2019s just <I>an<\/I> answer.<\/P>\n<P>OK, so what about the script itself? Well, we begin by defining a constant named ForReading; we\u2019ll use this constant later on when we open our log file. We then create an instance of the <B>Scripting.FileSystemObject<\/B> and use the <B>OpenTextFile<\/B> method to open the file C:\\Scripts\\Test.log. With the file open, we use the <B>ReadAll<\/B> method to read the entire contents of the file into a variable named strContents, and then we close Test.log.<\/P>\n<P>Got that? Next we assign the value 0 to a counter variable named i; we\u2019ll use i to keep a running tally of each instance of the word <I>Failure<\/I> that we run into. And then we have this line of code:<\/P><PRE class=\"codeSample\">arrLines = Split(strContents, &#8221; &#8220;)\n<\/PRE>\n<P>Remember how you said that you were stuck because your log file was all one big long line? Well, basically what we\u2019re doing here is dividing your log file (or at least the version of it stored in the variable strContents) into a whole bunch of little lines. In your log file individual words are separated by a blank space. In this line of code, we use the <B>Split<\/B> function to \u201csplit\u201d the value of strContents into an array; by splitting on the blank space (that is, by creating a new item in the array each time we encounter a blank space) we end up with an array that starts out like this:<\/P><PRE class=\"codeSample\">Failure \n2\/7\/2006 \n8:25 \nAM \nFailure \n2\/7\/2006 \n9:45 \nAM \nSuccess\n<\/PRE>\n<P>Sure, it looks funny, but now we can set up a For Each loop to run through each item in the array; more important, we can also use the <B>InStr<\/B> method to see if the word <I>Failure<\/I> can be found in any of those lines. If it can, we then increment the value of our counter variable i. All of that takes place within this block of code:<\/P><PRE class=\"codeSample\">For Each strLine in arrLines\n    If InStr(strLine, &#8220;Failure&#8221;) Then\n        i = i + 1\n    End If\nNext\n<\/PRE>\n<P>After we run through our For Each loop all we have to do is echo back our failures and we\u2019re done.<\/P>\n<P>Well, check that: all we have to do is echo back the number of failures found in the log file. Your script would likely time out long before it could finish echoing back all of <I>our<\/I> failures. (Although we <I>still<\/I> think <I>Scripting with Celebrities<\/I> would be far more entertaining than watching someone dance or ice skate with celebrities.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I count the number of times the word Failure appears in a log file? There\u2019s one catch, though: the log file simply writes its events one after another, creating one big, giant line of text.&#8212; FS Hey, FS. According to the rest of your email, your log file looks something [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[98,236,3,4,14,5],"class_list":["post-67993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-logs-and-monitoring","tag-plain-text-logs","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I count the number of times the word Failure appears in a log file? There\u2019s one catch, though: the log file simply writes its events one after another, creating one big, giant line of text.&#8212; FS Hey, FS. According to the rest of your email, your log file looks something [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67993"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67993\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}