{"id":64973,"date":"2007-04-30T21:31:00","date_gmt":"2007-04-30T21:31:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/30\/how-can-i-remove-the-first-five-lines-from-all-the-log-files-in-a-folder\/"},"modified":"2007-04-30T21:31:00","modified_gmt":"2007-04-30T21:31:00","slug":"how-can-i-remove-the-first-five-lines-from-all-the-log-files-in-a-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-the-first-five-lines-from-all-the-log-files-in-a-folder\/","title":{"rendered":"How Can I Remove the First Five Lines From All the Log Files in a Folder?"},"content":{"rendered":"<p><IMG 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\"> \n<P>Hey, Scripting Guy! I have about 400 log files in a folder. I need to go in and remove the first 5 lines from each of those log files. How can I write a script to do that?<BR><BR>&#8212; JW<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG 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 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> \n<P>Hey, JW. Before we begin we need to issue a clarification. <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0426.mspx\"><B>A few days ago<\/B><\/A> we reported that the Scripting Guy who writes this column had thrown a basketball the length of the court and made a basket; we dutifully termed this the greatest athletic feat in history. As it turns out, we were wrong: what this same Scripting Guy did last night was <I>far<\/I> more impressive than simply making a full-court shot in basketball.<\/P>\n<P>Interestingly enough, this latest achievement occurred while the Scripting Guy who writes this column was making dinner. The Scripting Stove is none-too-big, which means that, any time you have multiple pots and pans bubbling away, those pots and pans (especially the big ones) sit very close to one another. On top of that, the Scripting Guy who writes this column is none-too-bright; among other things, that means that he has a tendency to leave the handles of these pots and pans pointing out into the middle of the kitchen (as opposed to pointing them away from all the foot traffic).<\/P>\n<P>At any rate, somewhere along the line the Scripting Cook bumped into one of those handles, bumping it with enough force to send it skittering across the stove. Considering the fact that the stove is relatively small, that meant this pot bumped into a second pot, and suddenly both pots \u2013 and their contents \u2013 were headed off the stove and onto the floor.<\/P>\n<P>At that moment, in a feat worthy of Superman (or, at the very least, Aquaman) the Scripting Guy who writes this column snatched one pot out of mid-air with his left-hand, then somehow managed to grab the handle of the second pot just as it started to slide off the stove. In a split second the problem was solved, and without spilling a drop.<\/P>\n<P>But, really, that was no big deal: all in a day\u2019s work for the Scripting Guy who writes this column.<\/P>\n<P>Speaking of a day\u2019s work, maybe it\u2019s time we put in just that. JW needs a script that can remove the first five lines from every log file in a folder. Can we do that? No problem; it\u2019s as easy as boiling water.<\/P>\n<TABLE class=\"dataTable\" id=\"EKD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Boiling water <I>is<\/I> easy. It\u2019s keeping the pot of boiling water from crashing to the floor that\u2019s hard.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Let\u2019s start out by showing you a script that can remove the first five lines from a single text file; we\u2019ll then see if we can figure out how to modify that script so that it works against all the log files in a folder.<\/P>\n<P>Here\u2019s our initial script:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)<\/p>\n<p>i = 1<\/p>\n<p>Do Until objFile.AtEndOfStream\n    strLine = objFile.ReadLine\n    If i &gt; 5 Then\n        strContents = strContents &amp; strLine &amp; vbCrLf\n    End If\n    i = i + 1\nLoop<\/p>\n<p>objFile.Close<\/p>\n<p>Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\nobjFile.Write strContents<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>As you can see, we start out by defining a pair of constants (ForReading and ForWriting), constants we\u2019ll need when opening the text file. We then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> object and open the file C:\\Scripts\\Test.txt for reading:<\/P><PRE class=\"codeSample\">Set objFSo = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)\n<\/PRE>\n<P>Got that? Good. Now, we\u2019re assuming that our text file looks something like this:<\/P><PRE class=\"codeSample\">1\n2\n3\n4\n5\nA\nB\nC\nD\nE\nF\nG\n<\/PRE>\n<P>As it turns out, the first 5 lines are just header information; we need to delete those 5 lines and leave just the good stuff: ABCDEFG. But how are we going to do that?<\/P>\n<P>We\u2019ll tell you how. (After all, that <I>is<\/I> our job.) To begin with, we assign the value 1 to a counter variable named i. What are we going to do with this counter variable? You\u2019re about to find out.<\/P>\n<P>After initializing the counter variable we set up a Do Until loop designed to run until we reach the end of the text file (that is, until the file\u2019s <B>AtEndOfStream<\/B> property is True). Inside that loop, we use the <B>ReadLine<\/B> method to read the first line in the file, storing that value in a variable named strLine:<\/P><PRE class=\"codeSample\">strLine = objFile.ReadLine\n<\/PRE>\n<P>You know, you\u2019re right: we <I>don\u2019t<\/I> want the first line in the file, do we? For that matter, we don\u2019t want the next 4 lines, either. Fortunately we\u2019ve already thought of that:<\/P><PRE class=\"codeSample\">If i &gt; 5 Then\n<\/PRE>\n<P>What we\u2019re doing here is checking to see if our counter variable has a value greater than 5; as you might expect, we\u2019re using the counter variable to keep track of which line we just read in from the text file. The first time through the loop we\u2019re reading in line 1; it should come as no surprise, then, that the value of i is also 1.<\/P>\n<P>So what happens if i is <I>less<\/I> than 5? In that case, we must be dealing with one of the first 5 lines in the text file. We don\u2019t care about those lines so, in essence, we throw them away; we simply increment our counter variable by 1 (making i equal to 2), then loop around and repeat the entire process with line 2 in the text file. Because 2 isn\u2019t greater than 5 we\u2019ll end up ignoring line 2, then loop around and try again with line 3.<\/P>\n<P>Sooner or later we\u2019ll hit line 6; when we do, i will be <I>greater<\/I> than 5. In that case (and for every subsequent line), we take the value of strLine and assign it to a variable named strContents:<\/P><PRE class=\"codeSample\">strContents = strContents &amp; strLine &amp; vbCrLf\n<\/PRE>\n<P>Actually, that\u2019s only partly true, isn\u2019t it? What we <I>really<\/I> assign to strContents is the current value of strContents <I>plus<\/I> the value of strLine <I>plus<\/I> a carriage return-linefeed (vbCrLf). The net effect? Each time we read in a line from the text file we tack that line onto the end of strContents.<\/P>\n<P>After we\u2019ve read each line we close the file, then immediately reopen it, this time for writing:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\n<\/PRE>\n<P>At this point we call the <B>Write<\/B> method to write the value of strContents to the file, then close the file for good. What will Test.txt look like now? It\u2019ll look pretty much like it did before, except for one thing: the first 5 lines in the original file won\u2019t be there any more. In other words, Test.txt will now look like this:<\/P><PRE class=\"codeSample\">A\nB\nC\nD\nE\nF\nG\n<\/PRE>\n<P>How \u2018bout that?<\/P>\n<P>Of course, that only helps us with the file C:\\Scripts\\Test.txt; how are we going to perform this same task for <I>all<\/I> the log files in a folder? Like this, of course:<\/P><PRE class=\"codeSample\">Const ForReading = 1\nConst ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)<\/p>\n<p>For Each objLogFile In colFiles\n    If objLogFile.Extension = &#8220;log&#8221; Then\n        strFile = objLogFile.Name\n        Set objFile = objFSO.OpenTextFile(strFile, ForReading)<\/p>\n<p>        i = 1<\/p>\n<p>        Do Until objFile.AtEndOfStream\n            strLine = objFile.ReadLine\n            If i &gt; 5 Then\n                strContents = strContents &amp; strLine &amp; vbCrLf\n            End If\n            i = i + 1\n        Loop<\/p>\n<p>        objFile.Close<\/p>\n<p>        Set objFile = objFSO.OpenTextFile(strFile, ForWriting)\n        objFile.Write strContents<\/p>\n<p>        objFile.Close\n      End If\nNext\n<\/PRE>\n<P>If this looks somewhat similar to the script we just showed you, there\u2019s a good reason for that: it\u2019s pretty much the same script, just tricked out a bit so that it can work against all the log files in C:\\Scripts. As you can see, we start out by defining our constants then create an instance of the Scripting.FileSystemObject. After we do that, we connect to the WMI service on the local computer, then use this query to retrieve a collection of all the files in the folder C:\\Scripts:<\/P><PRE class=\"codeSample\">Set colFiles = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_Directory.Name=&#8217;C:\\Scripts&#8217;} Where &#8221; _\n        &amp; &#8220;ResultClass = CIM_DataFile&#8221;)\n<\/PRE>\n<P>What are we going to do with this collection? Well, to begin with, we\u2019re going to set up a For Each loop to walk through each item (that is, each file) in the collection. For each file we\u2019ll then check to see if the <B>Extension<\/B> is equal to <I>log<\/I> (without the dot):<\/P><PRE class=\"codeSample\">If objLogFile.Extension = &#8220;log&#8221; Then\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EJG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. You might not have to do this. For example, if you want to open <I>all<\/I> the files in the folder, regardless of file extension, then leave this part of the code out.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>If we <I>are<\/I> dealing with a log file we then assign the file <B>Name<\/B> (which is equivalent to the file path) to a variable named strFile:<\/P><PRE class=\"codeSample\">strFile = objLogFile.Name\n<\/PRE>\n<P>And now we <I>will<\/I> use the first script we showed you. The only difference? Instead of hard-coding the file name into our method calls we\u2019ll use the variable strFile to reference the file we want to work with. For example:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(strFile, ForReading)\n<\/PRE>\n<P>And guess what? This is going to open the first log file in the folder and remove the first 5 lines from that file. The script will then loop around, open the second log file, and remove the first 5 lines. This will continue until we\u2019ve opened up every log file in C:\\Scripts and removed the first 5 lines.<\/P>\n<P>Give it a try and see for yourself.<\/P>\n<P>Before we go we should issue one other clarification. Remember the Scripting Guy who writes this column? Well, as it turns out, his successes might be fewer and farther between then he lets on. Take the kitchen, for example. A more typical cooking session for him would be the one he had a few years ago, when he was making spaghetti. As it will sometimes do, a dab of the spaghetti sauce bubbled up and landed on top of the stove. The Scripting Guy who writes this column dutifully grabbed a dishrag and began wiping up the spilled sauce. As he did so, he apparently drug the dishrag over the burner; a second or two later, he realized that the dishrag he was holding in his hand was on fire.<\/P>\n<P>Which, in case you\u2019re wondering, is not the recommended state for a dishrag.<\/P>\n<P>And then there was the time he dumped noodles into the colander, only <I>then<\/I> realizing that he hadn\u2019t actually put the colander into the sink yet. And, of course, there was the time that \u2013 well, trust us: this could go on forever.<\/P>\n<P>And ever.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have about 400 log files in a folder. I need to go in and remove the first 5 lines from each of those log files. How can I write a script to do that?&#8212; JW Hey, JW. Before we begin we need to issue a clarification. A few days ago we [&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":[11,3,12,5],"class_list":["post-64973","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-folders","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have about 400 log files in a folder. I need to go in and remove the first 5 lines from each of those log files. How can I write a script to do that?&#8212; JW Hey, JW. Before we begin we need to issue a clarification. A few days ago we [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64973","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=64973"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64973\/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=64973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}