{"id":64683,"date":"2007-06-11T23:34:00","date_gmt":"2007-06-11T23:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/11\/how-can-i-clear-the-contents-of-a-text-file\/"},"modified":"2007-06-11T23:34:00","modified_gmt":"2007-06-11T23:34:00","slug":"how-can-i-clear-the-contents-of-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-clear-the-contents-of-a-text-file\/","title":{"rendered":"How Can I Clear the Contents of a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I clear the contents of a text file?<BR><BR>&#8212; JJ <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, JJ. Well, today is Monday, June 11<SUP>th<\/SUP>, and the Scripting Guys are finally back from a <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>week in Orlando<\/B><\/A>. 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\u2019s put it this way: if that\u2019s what you\u2019re thinking, then you\u2019re thinking wrong. <\/P>\n<P>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\u2019t say anything about Microsoft closing up shop he still hesitated for a moment; after all, maybe that just wasn\u2019t considered important enough to even <I>make<\/I> 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 <I>Hey, Scripting Guy!<\/I> for today. <\/P>\n<P>Hurray \u2026.<\/P>\n<P>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\u2019s them officially claim to have written a column even though they didn\u2019t put much effort into it. You know, a question like, say, how can I clear the contents of a text file? In case you\u2019re wondering, here\u2019s how:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)<\/p>\n<p>objFile.Write &#8220;&#8221;\nobjFile.Close\n<\/PRE>\n<P>Needless to say, there\u2019s not much to this script. We start out by defining a constant named ForWriting and setting the value to 2; we\u2019ll need this constant in order to open the file in \u201cfor writing\u201d mode. (And yes, we <I>do<\/I> have to open the file and write to it; that\u2019s because there\u2019s no command that directly erases the contents of a file.) We then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> and to open the file C:\\Scripts\\Test.txt for writing:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;, ForWriting)\n<\/PRE>\n<P>As we noted, there\u2019s no command (e.g., objFile.Erase) that we can call in order to erase the contents of a text file. But that\u2019s no big deal. After all, what exactly <I>is<\/I> an erased file? Depending on how you want to look at it, that\u2019s 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\u2019s what we do with this line of code:<\/P><PRE class=\"codeSample\">objFile.Write &#8220;&#8221;\n<\/PRE>\n<P>When we open a file for writing, anything we write to that file replaces everything currently in the file. That\u2019s a simple enough concept. In addition, the construction \u201c\u201d is VBScript\u2019s way of saying \u201cnothing;\u201d that \u201c\u201d 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!<\/P>\n<P>And no, we already thought of that: we tried using this script to erase the rest of this week, so that we wouldn\u2019t have to come in to work for awhile. It didn\u2019t work.<\/P>\n<P>Incidentally, if you\u2019re simply planning on re-using the same file over and over again (that is, each time you run a script) you don\u2019t 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 <I>do<\/I> need to erase a file or a set of files, well, now you know how.<\/P>\n<P>As for the Scripting Guys, the party\u2019s over, and it\u2019s 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\u2019s time to go home. OK, maybe coming back to work won\u2019t be so bad after all.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I clear the contents of a text file?&#8212; JJ 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5],"class_list":["post-64683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I clear the contents of a text file?&#8212; JJ 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64683","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64683\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}