How Can I Get a Script to Delete Itself?

How Can I Get a Script to Delete Itself?

  • Comments 1
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script for installing a printer that I give to my end users. When the script finishes, I’d like it to be able to delete itself. How can I get a script to delete itself?

-- EA

SpacerHey, Scripting Guy! AnswerScript Center

Hey, EA. You know, that’s an interesting idea: disposable scripts, just like disposable razors or disposable cameras. Actually, they’re better than disposable razors or disposable cameras. After all, when you’re 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.

Of course, for hundreds of years philosophers have been debating whether or not it’s possible 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’t 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’t go home until they invented the disposable script. About a minute later they came up with this:

Set objFSO = CreateObject("Scripting.FileSystemObject")

For i = 1 to 5
    Wscript.Echo i
    Wscript.Sleep 1000
Next

strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)

Actually it’s even easier 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 Scripting.FileSystemObject; this is the object we’ll use to delete the script. We then enter a For Next loop which - as we noted - doesn’t really have anything to do with the script itself; it’s just there so that something happens in between the time we start the script and the time when the script deletes itself:

For i = 1 to 5
    Wscript.Echo i
    Wscript.Sleep 1000
Next

The excitement begins after the For Next loop finishes. For starters, we get the value of Windows Script Host ScriptFullName property. That property returns the complete path to the script, which we then store in the variable strScript:

strScript = Wscript.ScriptFullName

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 DeleteFile method:

objFSO.DeleteFile(strScript)

What’s going to happen when the script runs? Well, it’s 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.

We don’t blame you: that does sound like a major problem just waiting to happen, doesn’t it? After all, if the script is still running and it deletes itself shouldn’t that cause some kind of problem?

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 - and then executes from - 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:

Set objFSO = CreateObject("Scripting.FileSystemObject")
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)

For i = 1 to 5
    Wscript.Echo i
    Wscript.Sleep 1000
Next

As you can see, here we start out by deleting the script and only then 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’t guarantee this approach will work with every script ever written, but it wouldn’t surprise us if it does.

Now, before we go here’s a historical fact most people aren’t 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’t know: everyone is still waiting for the disposable Scripting Guy to actually do some work. Apparently they made the thing a little too realistic, if you know what we mean.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Yes, but how do you do this with HTA?