Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I open a text file and replace the first 5 characters with the first 5 characters of the local computer name?

-- EC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, EC. You know, you might find this hard to believe (his fellow Scripting Guys absolutely refuse to believe it), but sometimes the Scripting Guy who writes this column is all talk and no action. For example, a few months ago he told everyone that he was planning on going back to college and getting a second Master’s degree (taking advantage of an email that promised he could get this degree in just two weeks). But was he really planning on going back to college? To tell you the truth, no: he’s way past the point where he ever wants to work hard or learn anything ever again.

Or so he thought. But then, last night, his life changed forever.

You might find this even harder to believe, but there was a time, long ago, when the Scripting Guy who writes this column was actually fairly smart; in fact, after he took his SAT test he was inundated with offers to attend universities all over the country. Harvard, Yale, Stanford, you name it. In the end, he opted to stay in the state of Washington, partly due to the costs involved, but largely because of the ho-hum factor. No offense to Harvard or Yale, but, to tell you the truth, places like that didn’t sound very exciting. As far as the Scripting Guy who writes this column was concerned, it didn’t matter: if you’ve seen one university you’ve seen them all.

Or so he thought. But then, last night, the Scripting Son was flipping through channels on the TV when he landed on the Food Network. It was there that the Scripting Guy who writes this column finally figured out what he was meant to do with his life: graduate from Dunkin’ Donuts University. That’s right, Dunkin’ Donuts University (founded in 1966), a school where the curriculum is based entirely on making – and eating – donuts. Just try to find courses on donut-making and donut-eating at one of those hoity-toity Ivy League colleges.

Well, OK, maybe Princeton. But other than that ….

There is a problem, however. Back when the Scripting Guy who writes this column was being inundated with invitations to attend various colleges and universities he did not receive an invitation from Dunkin’ Donuts U. Does that mean he isn’t qualified to attend Dunkin’ Donuts University? Considering the number of donuts he’s eaten in his life he expected, if anything, to be over-qualified. On the other hand, however, Dunkin’ Donuts is probably looking for smart, hard-working, and responsible people.

Dang! There’s always a catch, isn’t there?

Unless …. Hey, admissions committee at Dunkin’ Donuts University. How would you feel about a smart, hard-working and responsible guy who can write a script that can replace the first 5 characters of a text file with the name of the local computer?

Well, OK. Then how about this same script written by a not-so-smart, lazy, and somewhat-irresponsible guy:

Const ForReading = 1
Const ForWriting = 2

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

strContents = objFile.ReadAll
objFile.Close

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName
strComputer = Left(strComputer, 5)

intlength = Len(strContents)
strRemainder = Right(strContents, intLength - 5)
strNewContents = strComputer & strRemainder

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.WriteLine strNewContents

objFile.Close

On the off-chance that the admissions committee thinks the Scripting Guy who writes this column did nothing more than download this script off the Internet (like he did his master’s thesis) let’s take a few minutes to explain how this baby works. To begin with, we have a text file named C:\Scripts\Test.txt, a text file that (for our example purposes) looks like this:

XXXXX This is our text file. We want to replace the first five
characters (the Xs) with the first five characters of the
local computer's name.

As you can see, and as you can read for yourself, the first five characters in the text file are nothing but Xs. Our job is to replace those five characters with the first five characters in the name of the local computer.

To do that, we start out by defining a pair of constants, ForReading and ForWriting; we’ll need to use these two constants when we open Test.txt. (And yes, for better or worse we need to open the text file twice: once to read in the existing contents, the second time to replace those contents with our new, modified contents.) After defining the two constants we then use these two lines of code to create an instance of the Scripting.FileSystemObject and to open the text file for reading:

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

As soon as we have the file open we use the ReadAll method to read the contents of the file (storing that information in a variable named strContents), then we close Test.txt:

strContents = objFile.ReadAll
objFile.Close

Now what?

Funny you should ask that. (We hope that the admissions committee will take note of our prompt response to a customer inquiry.) Our next step is to run this block of code:

Set objNetwork = CreateObject("Wscript.Network")
strComputer = objNetwork.ComputerName

Here we’re creating an instance of the Wscript.Network object, then assigning the value of the ComputerName property to the variable strComputer. It probably doesn’t come as any great surprise that the value of the ComputerName property just happens to be the name of the computer itself. Of course, we don’t actually want the entire computer name; all we want are the first five characters. With that in mind we then use the Left function to grab those first five characters and store them in the variable strComputer:

strComputer = Left(strComputer, 5)

The net result? If we happen to be running this script on a computer named GIZMONIC (which we are) then strComputer will be equal to this:

GIZMO

We now have the first five characters in the computer name. Our next task is to replace the first five characters in the text file with these computer name characters. Depending on the kind of information in your text file you might be able to do this using a search-and-replace operation; that will work as long as the first five characters in the text fill will always be XXXXX, and as long as it’s OK to replace any other instances of XXXXX with GIZMO. We can’t be sure that those conditions will always hold true, so we decided to replace the first five characters using this approach:

intlength = Len(strContents)
strRemainder = Right(strContents, intLength - 5)
strNewContents = strComputer & strRemainder

In the first line of this code block we use the Len method to count the number of characters in strContents; if you’re keeping score at home, that’s 149. As you know, we want to replace only the first five characters in this string; we want to keep characters 6 through 149 exactly as they are. That’s what our second line of code is for; in that line we use the Right function to start at the end of the string and work our way backwards, scooping up characters as we go. How many characters do we scoop up? We want to grab a number equal to the length of the string minus 5. (149 – 5 equals 144, which means that we want to start at the end of the string and, working from right-to-left, grab the first 144 characters.) In turn, our variable strRemainder will be assigned everything except the first five characters in the string (because we stopped grabbing characters before we got to those).

Or, to put it another way, strRemainder ends up equal to this:

This is our text file. We want to replace the first five
characters (the Xs) with the first five characters of the
local computer's name.

As you can see, that’s the entire string except for the first five characters.

Finally, in line 3 we combine the variables strComputer and strRemainder, then assign that value to a variable named strNewContents. That makes this new variable equal to the following:

GIZMO This is our text file. We want to replace the first five
characters (the Xs) with the first five characters of the
local computer's name.

Well, what do you know: we’ve replaced the first five characters in the text file (XXXXX) with the first five characters in the computer name (GIZMO).

All that’s left now is to reopen the file Test.txt (this time for writing), use the WriteLine method to overwrite the old contents with our new, modified contents, and then close the file:

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForWriting)
objFile.WriteLine strNewContents

objFile.Close

That should do it.

We hope that helps, EC. (And we hope that impresses you, Dunkin’ Donuts University admissions committee.) We’d like to stay and take questions from the audience, but we need to hurry down to the local bakery and pick up a couple dozen donuts. After all, it’s never too early to start studying for our entrance exam.

At least not when you’re hoping to attend Dunkin’ Donuts University.