{"id":71243,"date":"2004-10-12T15:51:00","date_gmt":"2004-10-12T15:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/12\/how-can-i-count-the-number-of-lines-in-a-text-file\/"},"modified":"2004-10-12T15:51:00","modified_gmt":"2004-10-12T15:51:00","slug":"how-can-i-count-the-number-of-lines-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-count-the-number-of-lines-in-a-text-file\/","title":{"rendered":"How Can I Count the Number of Lines in a Text File?"},"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! How can I count the number of lines in a text file?<BR><BR>&#8212; MS<\/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, MS. It\u2019s easy to tell that Microsoft\u2019s scripting technologies weren\u2019t written by people who do system administration for a living. Our scripting technologies are jam-packed with features and capabilities, yet we always seem to missing the things that people <I>really<\/I> want to do. Counting the number of lines in a text file is a good example of that. This seems to be something system administrators do on a regular basis; after all, this is the fourth or fifth time we\u2019ve been asked this question just this month. And yet, there\u2019s no straightforward way to count the number of lines in a text. Is there a CountLines method somewhere? Nope. How about a NumberOfLines property? Nope. Go figure.<\/P>\n<P>Fortunately, while our scripting technologies aren\u2019t always straightforward, they usually <I>are<\/I> flexible enough to provide a workaround. And this is no exception. While there might not be an obvious way to count the number of lines in a text file, you can still use a script to get at this information. For example, this script returns the number of lines found in the file C:\\Scripts\\Test.txt:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objTextFile = objFSO.OpenTextFile _\n    (&#8220;C:\\Scripts\\Test.txt&#8221;, ForReading)<\/p>\n<p>objTextFile.ReadAll\nWscript.Echo &#8220;Number of lines: &#8221; &amp; objTextFile.Line\n<\/PRE>\n<P>So what\u2019s the secret here? Well, we begin by using the FileSystemObject to open the file for reading. Next we simply read the entire text file, using the ReadAll method. When we use ReadAll, we read in every line of the text file. Because the FileSystem object can only read from the beginning of a file to the end of the file, that means that when ReadAll is finished we must be on the very last line of the file; it\u2019s impossible for us to be anywhere else. Consequently, all we have to do is echo the value of the Line property, which reports the line number of the current line. Because we are on the last line, the Line property in this case also tells us the number of lines in the file. Simple.<\/P>\n<P>Of course, you might be thinking, \u201cOh, sure, open up and read an entire file just to get the line count? How long will <I>that<\/I> take?\u201d Surprisingly enough, not very long at all. We tested this script on a text file with just over 20,000 lines. On a regular old laptop computer (2.39 GhZ, 512 MB of RAM) the script took 1 second to complete. Your results might vary, but they probably won\u2019t vary by much<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I count the number of lines in a text file?&#8212; MS Hey, MS. It\u2019s easy to tell that Microsoft\u2019s scripting technologies weren\u2019t written by people who do system administration for a living. Our scripting technologies are jam-packed with features and capabilities, yet we always seem to missing the things that [&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":[3,4,14,5],"class_list":["post-71243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","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 lines in a text file?&#8212; MS Hey, MS. It\u2019s easy to tell that Microsoft\u2019s scripting technologies weren\u2019t written by people who do system administration for a living. Our scripting technologies are jam-packed with features and capabilities, yet we always seem to missing the things that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71243","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=71243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71243\/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=71243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}