How Can I Clear the Contents of a Text File?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I clear the contents of a text file?

— JJ

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JJ. Well, today is Monday, June 11th, and the Scripting Guys are finally back from a week in Orlando. After all that time spent lounging around in luxury hotels, eating in gourmet restaurants, and visiting theme parks you might think that the Scripting Guys would be excited about coming back to work. Let’s put it this way: if that’s what you’re thinking, then you’re thinking wrong.

In fact, when the Scripting Guy who writes this column got up this morning, the first thing he did was turn on the news, hoping to hear that Microsoft had gone out of business over the weekend. Even when they didn’t say anything about Microsoft closing up shop he still hesitated for a moment; after all, maybe that just wasn’t considered important enough to even make the morning news. Eventually, though, he drug himself into work, at which point he was struck by a terrifying thought: he had to write a Hey, Scripting Guy! for today.

Hurray ….

But you know what they say: when the going gets tough, the tough start looking around for a question to answer that they know the answer to off the tops of their heads, one that let’s them officially claim to have written a column even though they didn’t put much effort into it. You know, a question like, say, how can I clear the contents of a text file? In case you’re wondering, here’s how:

Const ForWriting = 2

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

objFile.Write “” objFile.Close

Needless to say, there’s not much to this script. We start out by defining a constant named ForWriting and setting the value to 2; we’ll need this constant in order to open the file in “for writing” mode. (And yes, we do have to open the file and write to it; that’s because there’s no command that directly erases the contents of a file.) We then use these two lines of code to create an instance of the Scripting.FileSystemObject and to open the file C:\Scripts\Test.txt for writing:

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

As we noted, there’s no command (e.g., objFile.Erase) that we can call in order to erase the contents of a text file. But that’s no big deal. After all, what exactly is an erased file? Depending on how you want to look at it, that’s simply an empty file, a file (like C:\Scripts\Test.txt) that exists but has nothing in it. Going by that definition, that means we can effectively erase the contents of a file simply by replacing those contents with, well, nothing. That’s what we do with this line of code:

objFile.Write “”

When we open a file for writing, anything we write to that file replaces everything currently in the file. That’s a simple enough concept. In addition, the construction “” is VBScript’s way of saying “nothing;” that “” represents a zero-length string. With that single line of code, therefore, we replace the existing contents of Test.txt with nothing. Lo and behold, the file has been erased!

And no, we already thought of that: we tried using this script to erase the rest of this week, so that we wouldn’t have to come in to work for awhile. It didn’t work.

Incidentally, if you’re simply planning on re-using the same file over and over again (that is, each time you run a script) you don’t necessarily have to erase the file; just opening the file for writing and writing new data to the file deletes the old contents and makes way for the new. But if you really do need to erase a file or a set of files, well, now you know how.

As for the Scripting Guys, the party’s over, and it’s time for us to get back to our daily routine. Which, now that we think about it, typically involves sitting around doing nothing, going to lunch, then sitting around doing nothing until it’s time to go home. OK, maybe coming back to work won’t be so bad after all.


Discussion is closed.

Feedback usabilla icon