How Can I Clear the Contents of a Text File?

How Can I Clear the Contents of a Text File?

  • Comments 10
  • Likes
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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • WOW.... Great  , i  was looking  for this... this helped me a lot .....

    Tks

  • Hey scripting guy! This isn't working for me!

    Obj.Write "" simply adds "" to the end of whatever text file it refers to.

  • @Frustrated scripter ... humm ..... well, using objfile.write "" should be a blank (empty) line ... I suspect that you have an extra pair of quotes or something going on ...

  • @all - The file must be opened "ForWriting" (mode 2) for the first write to clear the file.  Subsequent writes will then append.  If the file is opened in mode=8 (ForAppending) then the write will always append and obj.Write "" will append a blank line.

    If you always want a file with nothing in it including a blank line the you must use CreateTextFile.

    set f = fso.CreateTextFile( filename, True)

    f.Close

    This will overwrite the file with a blank file with no blank lines.  It will be a truly empty file.

  • Can I do this using wildcards? ex: Set objFile = objFSO.OpenTextFile("C:\Scripts\*..txt", ForWriting)

    or even *.* as all files in the folder will be modified.

    Thanks

    Jarrod

  • Any suggestions to my last post? I am trying to add a particular bit of code to several text files all of different types (text, php, js, html)

  • @Jarrod - all of the files you listed are text files.

    You cannot use wildcards in VBScript.  YOu can use wildcards in PowerShell.

    If you have issues then you need to post here:  •Scripting Forum:social.technet.microsoft.com/.../threads

  • Thanks for the reply jrv, but I am really disappointed in that, as I now have to learn another language.

  • @Jarrod

    Hey - the big boys uptown learn a language every week just to keep in shape. I learn a new language whenever I find one because it keeps me out of trouble.

    You should be happy that you get to learn something new.  VBScript is obsolete now anyway.  Don't waste your time on it.  It is much easier to do in PowerShell anyway.  This can be done in VBScript but it takes a very good technician to figure out how to do it.  In PosH it takes one line.

    Post all of your questions in the forum.  You will be surprised at what you find.

  • great, thanks.
    Const ForWriting = 2 is very important!