How Can I Count the Number of Characters in a Text File?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I count the number of characters in a text file?

— LD

SpacerHey, Scripting Guy! AnswerScript Center

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’s your answer: the Scripting Guy who writes this column doesn’t own a digital video recorder. For the most part, that’s because he’s a cheapskate; however, it’s also true that he’s not the least bit interested in any technology that enables you to skip commercials.

That’s 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’s house and takes care of his ailing friend. Talk about heart-warming, huh?

You bet. Well, except for the fact that even an elephant 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’s debit card.

But suppose a 27-year-old high school dropout who needed new wheels and tires for his ’67 Camaro found the zookeeper’s debit card? Or suppose a rogue elephant who needed new wheels and tires for his ’67 Camaro found the zookeeper’s debit card? In cases like that, well, we’re not sure that the ad would have been quite as heart-warming.

Note. The Scripting Guy who writes this column doesn’t 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.

Well, unless the elephant happens to be Horton. (“I meant what I said and I said what I meant: an elephant’s faithful, 100 percent.”)

And now for burning question number 2: how can we count the number of characters in a text file? Let’s assume that we have the following text file, which just happens to be a passage from Horton Hatches the Egg:

“It’s strange! It’s amazing! It’s wonderful! New!
Don’t shoot him! We’ll catch him! That’s just what we’ll do!
Let’s take him alive. Why he’s terribly funny!
We’ll sell him back home to a circus, for money!”

How are we going to count the characters in this text file? Like this:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForReading)

strCharacters = objFile.ReadAll

strCharacters = Replace(strCharacters, vbCrLf, “”) Wscript.Echo Len(strCharacters)

objFile.Close

To tell you the truth, we don’t 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’ll simply read the file and count the number of characters ourselves.

Note. OK, that’s not entirely true. It seems like overkill to us, but you can open the text file in Microsoft Word and then use Word’s ComputeStatistics method to determine the number of characters in the file. See this Office Space article for details.

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’ll 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 Scripting.FileSystemObject and, well, open the file C:\Scripts\Test.txt for reading:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFile = objFSO.OpenTextFile(“C:\Scripts\Test.txt”, ForReading)

As soon as the file is open we then use the ReadAll method to read the entire file and store the contents in a variable named strCharacters:

strCharacters = objFile.ReadAll

We’re practically done now. Why? Because once we have the contents of the file stashed away in a variable we can then use VBScript’s Len function to report back the number of characters in that variable. And that’s good, because the number of characters in the variable will equal the number of characters in the file.

So then why did we say that we were practically done? Well, if you were to manually count the characters in the file you’d 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’t see those characters and you probably don’t consider them to even be characters. But VBScript does.

Therefore, before we begin counting we use this line of code to get rid of any carriage return-linefeed characters:

strCharacters = Replace(strCharacters, vbCrLf, “”)

All we’re doing here is using the Replace function to search through strCharacters and replace all the carriage return-linefeeds (represented by the VBScript constant vbCrLf) with, well, nothing (“”). Needless to say, we could use this same approach to get rid of anything else we didn’t want to count as characters. For example, suppose you don’t want blank spaces counted as characters. No problem; just add in this line of code, which removes all the blank spaces from strCharacters:

strCharacters = Replace(strCharacters, ” “, “”)

Don’t like periods, question marks, or, say, semicolons? Now you know what to do about those problem characters, too.

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:

Wscript.Echo Len(strCharacters)

All that’s left now is to close the file and then go back to watching TV.

Speaking of which, we’re sure many of you are thinking, “Gosh, Scripting Guys, you already answer our scripting questions for us; you don’t have to watch TV for us as well.” All we can say is this: don’t worry about it. Someone 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’s a sacrifice we’re willing to make. Anything for our customers!

0 comments

Discussion is closed.

Feedback usabilla icon