{"id":65383,"date":"2007-03-06T01:10:00","date_gmt":"2007-03-06T01:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/03\/06\/how-can-i-count-the-number-of-characters-in-a-text-file\/"},"modified":"2007-03-06T01:10:00","modified_gmt":"2007-03-06T01:10:00","slug":"how-can-i-count-the-number-of-characters-in-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-count-the-number-of-characters-in-a-text-file\/","title":{"rendered":"How Can I Count the Number of Characters in a Text File?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I count the number of characters in a text file?<BR><BR>&#8212; LD<\/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, LD. We know a burning question for many of you (in addition to the question of how you can count the number of characters in a text file) is this: what kind of digital video recorder does the Scripting Guy who writes that column own? Well, here\u2019s your answer: the Scripting Guy who writes this column <I>doesn\u2019t<\/I> own a digital video recorder. For the most part, that\u2019s because he\u2019s a cheapskate; however, it\u2019s also true that he\u2019s not the least bit interested in any technology that enables you to skip commercials.<\/P>\n<P>That\u2019s right: the Scripting Guy who writes this column actually watches TV commercials. Last night, for example, he saw a new one about a kindly zookeeper who has a terrible cold. When the zookeeper leaves for home he accidentally drops his debit card. An elephant picks up the card, goes out and make a few purchases (like chicken soup), then heads over to the zookeeper\u2019s house and takes care of his ailing friend. Talk about heart-warming, huh?<\/P>\n<P>You bet. Well, except for the fact that even an <I>elephant<\/I> can find your debit card and use it, no questions asked: no one ever asked the elephant for ID, nor was the elephant required to enter a PIN number. (That, of course, is the whole point of this new card: no longer will you have to waste two seconds of your life typing in a four-digit PIN number.) This was a cute commercial because it was a friendly elephant who found and used the zookeeper\u2019s debit card. <\/P>\n<P>But suppose a 27-year-old high school dropout who needed new wheels and tires for his \u201967 Camaro found the zookeeper\u2019s debit card? Or suppose a rogue elephant who needed new wheels and tires for <I>his<\/I> \u201967 Camaro found the zookeeper\u2019s debit card? In cases like that, well, we\u2019re not sure that the ad would have been quite as heart-warming.<\/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><B>Note<\/B>. The Scripting Guy who writes this column doesn\u2019t work at a convenience store, but you can rest assured that, if he ever does, he will demand to see ID any time an elephant enters the store and tries to buy something using a debit card. You just never know with elephants.<\/P>\n<P>Well, unless the elephant happens to be <A href=\"http:\/\/www.amazon.com\/Horton-Hatches-Egg-Classic-Seuss\/dp\/039480077X\" target=\"_blank\"><B>Horton<\/B><\/A>. (\u201cI meant what I said and I said what I meant: an elephant\u2019s faithful, 100 percent.\u201d)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And now for burning question number 2: how can we count the number of characters in a text file? Let\u2019s assume that we have the following text file, which just happens to be a passage from <I>Horton Hatches the Egg<\/I>:<\/P><PRE class=\"codeSample\">&#8220;It&#8217;s strange! It&#8217;s amazing! It&#8217;s wonderful! New!\nDon&#8217;t shoot him! We&#8217;ll catch him! That&#8217;s just what we&#8217;ll do!\nLet&#8217;s take him alive. Why he&#8217;s terribly funny!\nWe&#8217;ll sell him back home to a circus, for money!&#8221;\n<\/PRE>\n<P>How are we going to count the characters in this text file? Like this:<\/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.txt&#8221;, ForReading)<\/p>\n<p>strCharacters = objFile.ReadAll<\/p>\n<p>strCharacters = Replace(strCharacters, vbCrLf, &#8220;&#8221;)\nWscript.Echo Len(strCharacters)<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>To tell you the truth, we don\u2019t know of any direct method of determining the number of characters in a text file (for example, binding to the file and then looking at some sort of NumberOfCharacters property). But are we going to let that stop us? Of course not. Instead, we\u2019ll simply read the file and count the number of characters ourselves.<\/P>\n<TABLE class=\"dataTable\" id=\"EGE\" 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>. OK, that\u2019s not <I>entirely<\/I> true. It seems like overkill to us, but you can open the text file in Microsoft Word and then use Word\u2019s <B>ComputeStatistics<\/B> method to determine the number of characters in the file. See this <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/officetips\/mar05\/tips0324.mspx\"><B>Office Space article<\/B><\/A> for details.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And just how are we going to count the number of characters in the file? Well, for starters, we define a constant named ForReading and set the value to 1; we\u2019ll use this constant when we open our text file for reading. We then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> and, well, 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>As soon as the file is open we then use the <B>ReadAll<\/B> method to read the entire file and store the contents in a variable named strCharacters:<\/P><PRE class=\"codeSample\">strCharacters = objFile.ReadAll\n<\/PRE>\n<P>We\u2019re practically done now. Why? Because once we have the contents of the file stashed away in a variable we can then use VBScript\u2019s <B>Len<\/B> function to report back the number of characters in that variable. And that\u2019s good, because the number of characters in the variable will equal the number of characters in the file.<\/P>\n<P>So then why did we say that we were <I>practically<\/I> done? Well, if you were to manually count the characters in the file you\u2019d probably come up with 204. However, our script is going to report back 210. Why? Because the script is going to include two characters (the carriage return and the linefeed characters) that occur at the end of lines 1, 2, and 3 in the file. You don\u2019t see those characters and you probably don\u2019t consider them to even <I>be<\/I> characters. But VBScript does.<\/P>\n<P>Therefore, before we begin counting we use this line of code to get rid of any carriage return-linefeed characters:<\/P><PRE class=\"codeSample\">strCharacters = Replace(strCharacters, vbCrLf, &#8220;&#8221;)\n<\/PRE>\n<P>All we\u2019re doing here is using the <B>Replace<\/B> function to search through strCharacters and replace all the carriage return-linefeeds (represented by the VBScript constant <B>vbCrLf<\/B>) with, well, nothing (\u201c\u201d). Needless to say, we could use this same approach to get rid of anything else we didn\u2019t want to count as characters. For example, suppose you don\u2019t want blank spaces counted as characters. No problem; just add in this line of code, which removes all the blank spaces from strCharacters:<\/P><PRE class=\"codeSample\">strCharacters = Replace(strCharacters, &#8221; &#8220;, &#8220;&#8221;)\n<\/PRE>\n<P>Don\u2019t like periods, question marks, or, say, semicolons? Now you know what to do about those problem characters, too.<\/P>\n<P>After deleting the carriage return-linefeeds (and any other unwanted characters) we finally get to use the Len function to report back the number of characters in the file:<\/P><PRE class=\"codeSample\">Wscript.Echo Len(strCharacters)\n<\/PRE>\n<P>All that\u2019s left now is to close the file and then go back to watching TV.<\/P>\n<P>Speaking of which, we\u2019re sure many of you are thinking, \u201cGosh, Scripting Guys, you already answer our scripting questions for us; you don\u2019t have to watch TV for us as well.\u201d All we can say is this: don\u2019t worry about it. <I>Someone<\/I> needs to tell you about all the latest commercials, and if that means the Scripting Guys have to spend their time watching TV, well, that\u2019s a sacrifice we\u2019re willing to make. Anything for our customers!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I count the number of characters in a text file?&#8212; LD Hey, LD. We know a burning question for many of you (in addition to the question of how you can count the number of characters in a text file) is this: what kind of digital video recorder does the [&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-65383","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 characters in a text file?&#8212; LD Hey, LD. We know a burning question for many of you (in addition to the question of how you can count the number of characters in a text file) is this: what kind of digital video recorder does the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65383","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=65383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65383\/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=65383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}