January 2nd, 2007

How Can I Append a Blank Line to a Text File, Then Append the Current Date and Time?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I append a blank line to a text file, then append the current date and time?

— PL

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PL. You know, back in the 1600s people were inordinately worried about witches and witchcraft. Fortunately, though, they developed a sure-fire test to determine whether or not someone was a witch: the accused was weighted down with rocks, then tossed into a lake. If they floated back up to the top they were a witch; after all, no one can float in a lake if they’ve been weighted down with rocks, at least not unless they were in league with the Devil. In that case the accused would be hauled out of the lake and hung. (No, we’re not completely sure why their magic would work when weighted down with rocks and tossed in a lake, yet be useless when faced with the hangman’s noose.)

If you sank to the bottom and died, well, that was actually good; that meant you weren’t a witch. In turn, that also meant that you were entitled to an honorable Christian burial. Hooray!

Now, you might think that people were troubled by this: after all, if you were accused of witchcraft then, one way or the other, you were going to end up dead. But, for the most part, people didn’t lose much sleep over the matter. After all, if you were truly innocent then no one would accuse you of being a witch, would they? To be blunt, this was all your fault: you must have done something to warrant such serious accusations.

So how did an innocuous little question about text files get us started thinking about things like the Salem Witch Trials? That’s easy: when we saw a question about modifying a text file we immediately launched into accusatory mode. “You know, that’s way harder than you might think. There’s really no easy and obvious way to do this. Don’t blame us; the FileSystemObject wasn’t designed to handle situations like this.” Without even blinking we ran through all our standard warnings, caveats and excuses about why it’s so hard to modify the text in a text file.

It was only after we unleashed this torrent of accusations that we realized that you don’t really want to modify a text file, you just want to tack some additional information onto the tail end of that file. As it turns out, that is easy:

Const ForAppending = 8

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

objFile.WriteLine objFile.WriteLine objFile.Write Now

objFile.Close

By the way, sorry about weighting you down with rocks and tossing you in the lake just because we thought you were asking a question about modifying text files. Would it help if we offered you an honorable Christian burial?

That’s what we were afraid you’d say. OK, then what do you say we explain how this script works? Let’s assume that we have a text file that looks like this:

A
B
C

Is our script really going to be able to add a blank line and a timestamp to the end of this file? Of course, it – hey, wait a minute: that sounds like something a witch would say. Hmmm ….

Actually there’s very little magic involved here. We begin by defining a constant named ForAppending and setting to value to 8; we’ll use this constant to specify the correct mode when opening our text file. We create an instance of the Scripting.FileSystemObject, then use the OpenTextFile method to open the file C:\Scripts\Test.txt for appending:

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

Note. Why “for appending?” That’s easy. If you open a file for writing the FileSystemObject will erase the existing contents of the file and replace the old contents with whatever new stuff you’re adding. When you open a file for appending, however, new content is simply tacked on to the end of the file.

That was pretty easy; believe it or not, from here on it gets even easier. To add a blank line to the file we call the WriteLine method twice, without supplying any parameters; each time we call WriteLine in that fashion (that is, without parameters) that’s equivalent to pressing ENTER on the keyboard. (And pressing ENTER twice will give you a blank line in a document.) We then call the Write method, appending the value of the VBScript Now function to the file. Because Now provides us with the current date and time, we end up with a file that looks like this:

A
B
C

1/2/2007 8:07:21 AM

All that’s left now is to close the file and call it a day.

Before we go, we feel compelled to note that we 21st century types shouldn’t feel smug and superior towards people of the 1600s; the truth is, things haven’t changed all that much in the past 400 years. For example, that part about weighting people down with rocks, tossing them in a lake, and then standing around waiting to see what happens? That’s the exact same method Microsoft uses when conducting annual performance reviews. Like they say, the more things change the more they stay the same.

Editor’s Note: No, Microsoft doesn’t really do that. That would mean everyone who survives a review manages to float to the top, and we certainly wouldn’t want anyone to jump to any conclusions as to how they managed to do that.


Author

0 comments

Discussion are closed.