{"id":66243,"date":"2006-10-17T12:00:00","date_gmt":"2006-10-17T12:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/10\/17\/how-can-i-clear-the-contents-of-any-file-dropped-on-my-script-icon\/"},"modified":"2006-10-17T12:00:00","modified_gmt":"2006-10-17T12:00:00","slug":"how-can-i-clear-the-contents-of-any-file-dropped-on-my-script-icon","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-clear-the-contents-of-any-file-dropped-on-my-script-icon\/","title":{"rendered":"How Can I Clear the Contents of Any File Dropped on My Script Icon?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I write a script that will clear the contents of any file that I drag and drop on the script icon?<BR><BR>&#8212; SN<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, SN. Say, you didn\u2019t by any chance misplace a few hundred thousand insects did you? OK; just thought we\u2019d ask. This past weekend the Scripting Guy who writes this column had to drive across the state to attend a wedding. As the family was tooling along, on a bright, sunny day, without a cloud in the sky, it suddenly started to rain. Or maybe it was hail. At first all they knew was that \u2013 out of the blue \u2013 the windshield was being pelted by \u2026 something. \u201cHow can it rain when there aren\u2019t any clouds in the sky?\u201d asked the Scripting Son.<\/P>\n<P>\u201cThat\u2019s not rain,\u201d said the all-wise and all-knowing Scripting Dad. \u201cThose are bugs.\u201d<\/P>\n<P>Sure enough, the Scripting Windshield had been assaulted by thousands and thousands of kamikaze bugs, with so many hitting the windshield it sounded like raindrops (although after a minute or so all you had to do was look at the windshield and you could tell that it wasn\u2019t raining). Definitely one of the most surreal experiences the Scripting Guy who writes this column has been through lately.<\/P>\n<P>Which is saying quite a bit considering that he works at Microsoft.<\/P>\n<P>At any rate, SN, if you <I>did<\/I> misplace a bunch of insects, well, we know where you can find them. <\/P>\n<P>Or at least what\u2019s left of them.<\/P>\n<TABLE id=\"EED\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. OK, technically we <I>did<\/I> just plow through the mass of insects without trying to avoid them; to make matters worse, we then continued driving without stopping to help the survivors. Cruel and callous? Maybe. But we\u2019re not going to apologize for that. After all, <I>they<\/I> started it.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, we can\u2019t be sure that those were your bugs, SN; it was hard to read their little nametags at 70 miles per hour. If they <I>were<\/I> yours, however, well, we\u2019d like to try and make it up to you. What do you say we show you a script that can erase the contents of any file that is dragged and dropped on the script icon? Would that help?<\/P>\n<P>Let\u2019s hope so:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>For Each strArgument in Wscript.Arguments\n    Set objFile = objFSO.OpenTextFile(strArgument, ForWriting)\n    objFile.Write &#8220;&#8221;\n    objFile.Close\nNext\n<\/PRE>\n<P>As you can see, there\u2019s really not much to the script. (But, then again, it\u2019s not like we ran over 100,000 dogs or cats or something.) We start off by defining a constant named ForWriting and setting the value to 2; we\u2019ll use this constant when we open the file (or files) that have been dropped on the script icon. Once we\u2019ve defined our constant we then create an instance of the <B>Scripting.FileSystemObject<\/B>, the scripting technology of choice (OK, the scripting technology of <I>necessity<\/I>) when it comes to working with text files. (We\u2019re assuming you want to erase text files, a topic we\u2019ll revisit in a few minutes.)<\/P>\n<P>Now comes the fun part. One of the more interesting \u2013 albeit underutilized \u2013 features of Windows Script Host is this: if you drag and drop a file (or group of files) on a script icon (that is, on the .VBS file in Windows Explorer) the complete path name for any dropped file is automatically passed to the script as a command-line argument. That means that the script knows exactly which files have been dragged and dropped on the script icon. That capability turns out to be the secret ingredient for today\u2019s script.<\/P>\n<P>With that in mind, we next set up a For Each loop that loops through the <B>Wscript.Arguments<\/B> collection, the collection of command-line arguments passed to the script. Inside that loop we use the <B>OpenTextFile<\/B> method to, one-by-one, open each of the files that were dragged on top of our script icon:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.OpenTextFile(strArgument, ForWriting)\n<\/PRE>\n<P>Nothing very fancy here: we simply call OpenTextFile and pass it the path to the file we want to open (strArgument) followed by the constant ForWriting (which tells the FileSystemObject that we want to write to the file). <\/P>\n<P>As it turns out, the FileSystemObject doesn\u2019t have any sort of pre-defined method for erasing the contents of a file. But that\u2019s OK; we can handle that ourselves. When you open a file for writing, anything you write will completely replace the existing contents of that file. We want to erase the file, which means we want an empty file, one that doesn\u2019t <I>have<\/I> any contents. Therefore, all we have to do is write an empty string to the file, like so:<\/P><PRE class=\"codeSample\">objFile.Write &#8220;&#8221;\n<\/PRE>\n<P>That replaces the existing contents of the file with nothing, effectively erasing everything that came before. (Thanks; we thought it was clever, too.) We then close the file, loop around, and repeat the process with the next argument in the collection.<\/P>\n<P>Having shown you the code we now feel compelled to issue a warning: be careful when you use this script. Ostensibly it\u2019s designed to work only with text files; after all, that\u2019s what the FileSystemObject is for. However, this script <I>can<\/I> erase other types of files: we tried it with Word documents, Excel spreadsheets, .ZIP archives, and .PDF files, and the script succeeded in erasing each and every file we dropped on it. That\u2019s not bad, mind you; it\u2019s just something to keep in mind. If you drag a file onto this script you can pretty much count on that file getting erased. The file itself remains; you\u2019ll still have a file named C:\\Scripts\\Test.txt. It\u2019s just that the file will now be empty.<\/P>\n<TABLE id=\"E2E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We won\u2019t show you how to do this today, but it would be easy enough to have the script prompt you before deleting a file (e.g., \u201cAre you sure you want to delete file <I>x<\/I>?\u201d). In fact, in digging through the <I>Hey, Scripting Guy!<\/I> archives we uncovered <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/aug04\/hey0816.mspx\"><B>a column<\/B><\/A> that explains how to do something very similar.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In case you\u2019re wondering, the fact that the Scripting Guy who writes this column was quickly able to identify the culprit as being a huge mass of bugs, as opposed to rain or hail or something else, wasn\u2019t really that big of a deal. Like we said, he <I>does<\/I> work at Microsoft, and who would know more about a huge mass of bugs than someone who works at Microsoft?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I write a script that will clear the contents of any file that I drag and drop on the script icon?&#8212; SN Hey, SN. Say, you didn\u2019t by any chance misplace a few hundred thousand insects did you? OK; just thought we\u2019d ask. This past weekend the Scripting Guy who [&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":[40,22,2,3,4,14,5],"class_list":["post-66243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-retrieving-input","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I write a script that will clear the contents of any file that I drag and drop on the script icon?&#8212; SN Hey, SN. Say, you didn\u2019t by any chance misplace a few hundred thousand insects did you? OK; just thought we\u2019d ask. This past weekend the Scripting Guy who [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66243","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=66243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66243\/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=66243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}