{"id":67253,"date":"2006-05-24T07:47:00","date_gmt":"2006-05-24T07:47:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/05\/24\/how-can-i-get-a-script-to-delete-itself\/"},"modified":"2006-05-24T07:47:00","modified_gmt":"2006-05-24T07:47:00","slug":"how-can-i-get-a-script-to-delete-itself","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-get-a-script-to-delete-itself\/","title":{"rendered":"How Can I Get a Script to Delete Itself?"},"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! I have a script for installing a printer that I give to my end users. When the script finishes, I\u2019d like it to be able to delete itself. How can I get a script to delete itself?<BR><BR>&#8212; EA<\/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, EA. You know, that\u2019s an interesting idea: disposable scripts, just like disposable razors or disposable cameras. Actually, they\u2019re <I>better<\/I> than disposable razors or disposable cameras. After all, when you\u2019re done with a disposable camera you still have to throw it away yourself. But the disposable script throws itself away! You could be on to something here, EA.<\/P>\n<P>Of course, for hundreds of years philosophers have been debating whether or not it\u2019s <I>possible<\/I> to create a disposable script; what makes the Scripting Guys think we could create such a thing on our first try? To be honest, we weren\u2019t sure that we could, but we assigned all the scientists and technicians at Scripting Guys Laboratories to work on the problem anyway, telling hundreds of researchers that they couldn\u2019t go home until they invented the disposable script. About a minute later they came up with this:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>For i = 1 to 5\n    Wscript.Echo i\n    Wscript.Sleep 1000\nNext<\/p>\n<p>strScript = Wscript.ScriptFullName\nobjFSO.DeleteFile(strScript)\n<\/PRE>\n<P>Actually it\u2019s even <I>easier<\/I> than it looks; after all, over the half the lines of code are there just so the script does something before it deletes itself. We begin by creating an instance of the <B>Scripting.FileSystemObject<\/B>; this is the object we\u2019ll use to delete the script. We then enter a For Next loop which &#8211; as we noted &#8211; doesn\u2019t really have anything to do with the script itself; it\u2019s just there so that something happens in between the time we start the script and the time when the script deletes itself:<\/P><PRE class=\"codeSample\">For i = 1 to 5\n    Wscript.Echo i\n    Wscript.Sleep 1000\nNext\n<\/PRE>\n<P>The excitement begins after the For Next loop finishes. For starters, we get the value of Windows Script Host <B>ScriptFullName<\/B> property. That property returns the complete path to the script, which we then store in the variable strScript:<\/P><PRE class=\"codeSample\">strScript = Wscript.ScriptFullName\n<\/PRE>\n<P>As you might expect, strScript now contains a path similar to this: C:\\Scripts\\My_Script.vbs. Once we have the complete path we can then delete the script simply by passing the variable strScript to the <B>DeleteFile<\/B> method:<\/P><PRE class=\"codeSample\">objFSO.DeleteFile(strScript)\n<\/PRE>\n<P>What\u2019s going to happen when the script runs? Well, it\u2019s going to echo the numbers 1 through 5 to the screen (pausing one second between each echo). As soon as the script has finished with that chore it will then delete itself. Just like that.<\/P>\n<P>We don\u2019t blame you: that <I>does<\/I> sound like a major problem just waiting to happen, doesn\u2019t it? After all, if the script is still running and it deletes itself shouldn\u2019t that cause <I>some<\/I> kind of problem?<\/P>\n<P>Well, it probably would if the script had to continually read from its .vbs file. However, when you run a script the entire script is loaded into &#8211; and then executes from &#8211; memory. At that point the .vbs file becomes irrelevant; the script no longer needs it or refers to it. For example, take a look at the following script:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nstrScript = Wscript.ScriptFullName\nobjFSO.DeleteFile(strScript)<\/p>\n<p>For i = 1 to 5\n    Wscript.Echo i\n    Wscript.Sleep 1000\nNext\n<\/PRE>\n<P>As you can see, here we start out by deleting the script and only <I>then<\/I> do we enter the For Next loop and echo numbers back to the screen. Can a script really run even though the script file has already been deleted? Give it a try and see for yourself. We can\u2019t guarantee this approach will work with every script ever written, but it wouldn\u2019t surprise us if it does.<\/P>\n<P>Now, before we go here\u2019s a historical fact most people aren\u2019t aware of. A few years ago Microsoft actually tried developing a disposable Scripting Guy. The idea was that the disposable Scripting Guy would do his or her work and then automatically delete itself. So how did that turn out? To tell you the truth, we don\u2019t know: everyone is still waiting for the disposable Scripting Guy to actually <I>do<\/I> some work. Apparently they made the thing a little <I>too<\/I> realistic, if you know what we mean.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have a script for installing a printer that I give to my end users. When the script finishes, I\u2019d like it to be able to delete itself. How can I get a script to delete itself?&#8212; EA Hey, EA. You know, that\u2019s an interesting idea: disposable scripts, just like disposable razors [&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,2,3,4,14,5],"class_list":["post-67253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have a script for installing a printer that I give to my end users. When the script finishes, I\u2019d like it to be able to delete itself. How can I get a script to delete itself?&#8212; EA Hey, EA. You know, that\u2019s an interesting idea: disposable scripts, just like disposable razors [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67253","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=67253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67253\/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=67253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}