{"id":55563,"date":"2008-05-15T01:41:00","date_gmt":"2008-05-15T01:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/05\/15\/hey-scripting-guy-how-can-i-search-a-text-file-for-activities-that-took-place-on-a-specified-date\/"},"modified":"2008-05-15T01:41:00","modified_gmt":"2008-05-15T01:41:00","slug":"hey-scripting-guy-how-can-i-search-a-text-file-for-activities-that-took-place-on-a-specified-date","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-search-a-text-file-for-activities-that-took-place-on-a-specified-date\/","title":{"rendered":"Hey, Scripting Guy! How Can I Search a Text File for Activities That Took Place on a Specified Date?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I\u2019m trying to write a script that can go through a log file and determine the number of users who logged off on a particular day. Can you help me?<br \/>&#8212; TM<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, TM. You know, when you work at Microsoft people think you probably come to work in your pajamas, ride a unicycle through the hallways, and do all those other weird and wacky things that people in the high-tech industry do. As it turns out, though, that\u2019s not the case, not at all. (And thank goodness: it\u2019s bad enough seeing people around here in their shorts, let alone in their pajamas.) <\/p>\n<p>Instead, Microsoft is just like any other workplace. For example, often-times people around here leave you absolutely speechless with their \u2026 ideas \u2026. This is a problem the Scripting Guys have been grappling with for over a year now. \u201cHey, guys,\u201d we\u2019ve been told over and over. \u201cRemember how, under the old system, you were able to do 100 different things? Well now, under our new system, we\u2019ve improved things to the point where you\u2019ll only be able to do 12 of those things! Isn\u2019t that great!?!\u201d<\/p>\n<p>We\u2019ll have to get back to you on that.<\/p>\n<table class=\"dataTable\" id=\"E3C\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. Of course, dealing with certain people here <i>has<\/i> caused the Scripting Guy who writes this column to completely revise his career goals. His goal now is to become the person who hands over the check to the winner of the Washington state lottery. With any luck at all, one of these Microsoft people will win the lottery, and when he goes to hand them the check the Scripting Guy who writes this column will say, \u201cRemember how you won $10 million in the Washington state lottery? Well, just think: if you give me $9 million of those dollars, you\u2019ll still be able to keep a whole million for yourself! Isn\u2019t that great!?!\u201d<\/p>\n<p>And sure, it\u2019s a bit of a long shot. But, then again, his so-called \u201creal\u201d retirement plan (e.g., 401K and stock options) isn\u2019t doing much better.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Anyway, we mention all this simply because the Scripting Guys have a meeting later today that could go a long way to determining the future of the Script Center and of scripting documentation in general. Are we looking forward to this meeting? Let\u2019s put it this way: when it comes to meetings, we\u2019d prefer to see everyone come to work in their pajamas.<\/p>\n<p>No, wait, we take that back. After all, there\u2019s always a chance that some of these people don\u2019t <i>wear<\/i> pajamas.<\/p>\n<p>At any rate, the Scripting Guy is awfully busy today, and he\u2019s in an even worse mood than usual. But does that mean he\u2019s too busy and too grouchy to write a script that can tally logon\/logoff data using information taken from a text file? Yes.<\/p>\n<p>Oh, wait; we mean, no:<\/p>\n<pre class=\"codeSample\">Const ForReading = 1\n\nintLoggedOn = 0\nintLoggedOff = 0\n\nSet objFSO = CreateObject(\"Scripting.FileSystemObject\")\nSet objFile = objFSo.OpenTextFile(\"C:\\Scripts\\Test.txt\", ForReading)\n\nDo Until objFile.AtEndOfStream\n    strLine = objFile.ReadLine\n    If InStr(strLine, \"2008-04-04\") Then\n        If InStr(strLine, \"logged on\") Then\n            intLoggedOn = intLoggedOn + 1\n        ElseIf InStr(strLine, \"logged off\") Then\n            intLoggedOff = intLoggedOff + 1\n        End If\n    End If\nLoop\n\nWscript.Echo \"Logons: \" &amp; intLoggedOn\nWscript.Echo \"Logoffs: \" &amp; intLoggedOff\n<\/pre>\n<table class=\"dataTable\" id=\"EUD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p><b>Note<\/b>. True, a <i>real<\/i> Microsoft employee might tell you, \u201cRemember that script that was supposed to tally logon\/logoff data using information taken from a text file? Well, instead, we wrote you a script that <i>doesn\u2019t<\/i> tally logon\/logoff data using information taken from a text file. Isn\u2019t that great!?!\u201d Fortunately, though, the Scripting Guys are usually not considered <i>real<\/i> Microsoft employees.<\/p>\n<p>Especially by other Microsoft employees.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Our script \u2013 which, to set your mind at ease, really <i>does<\/i> tally logon\/logoff data using information taken from a text file \u2013 starts off by defining a constant named ForReading and setting the value to 1; we\u2019ll use this constant when we open up and read the text file. Speaking of text files, the sample file TM sent us looks something like this:<\/p>\n<pre class=\"codeSample\">29        00000001  2008-04-04 10:17:34  Server successfully initialized.\n30        00000001  2008-04-04 10:17:42  Connection made from workstation atl-ws-001 (CID:14537, API:1.25).\nClient: ServerAdministration; SDK 8.0.0.74; JVM 1.5.0_03-b07; Windows 2003\n31        00000001  2008-04-04 10:17:42  User StarTeam Server Administrator logged on connection from workstation StarTeamApp01 (CID:14537).\n32        00000001  2008-04-04 10:20:31  Connection made from workstation atl-ws-002 (CID:4272, API:1.24).\nClient: StarTeam 7.0.131; Windows XP\n33        00000001  2008-04-04 10:20:31  User Ken Myer logged on connection from workstation atl-ws-002 (CID:4272).\n34        00000001  2008-04-04 10:21:20  User Ken Myer logged off connection from workstation atl-ws-002 (CID:4272).\n35        00000001  2008-04-04 11:00:56  Connection made from workstation atl-ws-003 (CID:7800, API:1.24).\nClient: StarTeam 7.0.131; Windows XP\n36        00000001  2008-04-04 11:00:56  User Pilar Ackerman logged on connection from workstation atl-ws-003 (CID:7800).\n<\/pre>\n<p>If you look through this sample test, you\u2019ll see that we have two logons and one logoff for April 4, 2008:<\/p>\n<pre class=\"codeSample\">33        00000001  2008-04-04 10:20:31  User Ken Myer logged on connection from workstation atl-ws-002 (CID:4272).\n34        00000001  2008-04-04 10:21:20  User Ken Myer logged off connection from workstation atl-ws-002 (CID:4272).\n36        00000001  2008-04-04 11:00:56  User Pilar Ackerman logged on connection from workstation atl-ws-003 (CID:7800).\n<\/pre>\n<p>Our job is simple: we need to programmatically count up the total number of logons and logoffs for a given date.<\/p>\n<p>So how are we going to do that? Well, after we define our constant we\u2019re going to assign the value 0 to a pair of variables, intLoggedOn and intLoggedOff:<\/p>\n<pre class=\"codeSample\">intLoggedOn = 0\nintLoggedOff = 0\n<\/pre>\n<p>Once that\u2019s done we create an instance of the <b>Scripting.FileSystemObject<\/b>, then use this line of code to open the text file C:\\Scripts\\Test.txt for reading:<\/p>\n<pre class=\"codeSample\">Set objFile = objFSo.OpenTextFile(\"C:\\Scripts\\Test.txt\", ForReading)\n<\/pre>\n<p>Our next step is to set up a Do Until loop designed to run until we\u2019ve reached the end of the file; that is, until the file\u2019s <b>AtEndOfStream<\/b> property is True. Inside that loop, the first thing we do is use the <b>ReadLine<\/b> method to read the initial line in the file and store it in a variable named strLine:<\/p>\n<pre class=\"codeSample\">strLine = objFile.ReadLine\n<\/pre>\n<p>At this point we need to do two things. First, we need determine whether or not the action recorded on this line of code took place on April 4, 2008. We can do that by using VBScript\u2019s <b>InStr<\/b> function to search for the date-time value 2008-04-04:<\/p>\n<pre class=\"codeSample\">If InStr(strLine, \"2008-04-04\") Then\n<\/pre>\n<p>If InStr returns a 0 that means that the date-time value 2008-04-04 couldn\u2019t be found; therefore we simply go back to the top of the loop and repeat the process with the next line in the text file. If the date-time value <i>was<\/i> found we then need to check to see if either a logon or a logoff took place. That\u2019s what this block of code is for:<\/p>\n<pre class=\"codeSample\">If InStr(strLine, \"logged on\") Then\n    intLoggedOn = intLoggedOn + 1\nElseIf InStr(strLine, \"logged off\") Then\n    intLoggedOff = intLoggedOff + 1\nEnd If\n<\/pre>\n<p>As you can see, in line 1 we\u2019re again using InStr to search the line of text for a particular phrase; in this case, that\u2019s the phrase <i>logged on<\/i>. What if we find the search phrase? Well, that means we\u2019re dealing with a logon; in turn, we increment the value of the variable intLoggedOn by 1:<\/p>\n<pre class=\"codeSample\">intLoggedOn = intLoggedOn + 1\n<\/pre>\n<p>What if we <i>don\u2019t<\/i> find the search phrase? Well, in that case we try again, this time looking for the string <i>logged off<\/i>:<\/p>\n<pre class=\"codeSample\">ElseIf InStr(strLine, \"logged off\") Then\n<\/pre>\n<p>If found, we increment the value of the variable intLoggedOff by 1. Either way, it\u2019s then back to the top of the loop, where we try, try again with the next line in the text file.<\/p>\n<p>After we\u2019ve read each line in the text file we echo back the values of intLoggedOn and intLoggedOff. That\u2019s going to give us output similar to this:<\/p>\n<pre class=\"codeSample\">Logons: 2\nLogoffs: 1\n<\/pre>\n<p>And there you have it.<\/p>\n<p>That\u2019s all we have time for today; we need to straighten our pajamas, hop on our unicycles, and head off to a meeting. To be honest, we don\u2019t expect anything bad to come out of this meeting, and we expect that life will go on the same as ever. But you never know; after all, this <i>is<\/i> Microsoft: \u201cRemember that Script Center thing where you can find all sorts of information about system administration scripting? Well, now we have a Script Center where you can\u2019t find <i>any<\/i> information about system administration scripting! Isn\u2019t that great!?!\u201d <\/p>\n<p>We\u2019ll keep you posted.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m trying to write a script that can go through a log file and determine the number of users who logged off on a particular day. Can you help me?&#8212; TM Hey, TM. You know, when you work at Microsoft people think you probably come to work in your pajamas, ride a [&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":[13,3,4,14,5],"class_list":["post-55563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019m trying to write a script that can go through a log file and determine the number of users who logged off on a particular day. Can you help me?&#8212; TM Hey, TM. You know, when you work at Microsoft people think you probably come to work in your pajamas, ride a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55563","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=55563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/55563\/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=55563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=55563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=55563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}