Skip to: Site menu | Main content


Remember me?

Making an exe Delete Itself After Execution

written by Mark Rowlinson - Last updated Nov 2004

The idea behind this code is to create a batch file to delete the exe when the exe has closed. The sub should be called as the last thing before the app ends.

Private Sub DeleteMe() 
     'create a textfile called DelMe.bat
    Dim fNum As Long 
    fNum = FreeFile 
    Open App.Path + "DelMe.bat" For Output As #fNum 
     'stop any output to the screen
    Print #fNum, "@echo off" 
     'line name
    Print #fNum, ":start" 
     'attempt to delete the app
    Print #fNum, "del " + App.EXEName + ".exe" 
     'if unsuccessful try again
    Print #fNum, "if exist " + App.EXEName + ".exe goto start" 
     'now delete the batch file
    Print #fNum, "del " + App.Path + "DelMe.bat" 
     'close the file
    Close #fNum 
     'finally shell the batch file
    Shell App.Path + "DelMe.bat", vbHide 
End Sub