mrcool4444
asked on
Delete program after ran
How would I delete my program after it ran? I've seen this code on planetsourcecode, but can't find it anymore.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with rspahitz, how can a program delete itself.
In rspahitz example, you are still left with the "killme" app that is not deleted...
I also agree with sirnick, but again, I think you will get an error because the batch file will run before the main .exe is out of memory and will not delete it....
How I would attempt this is to have your app build a vbs script file that is called and deletes the 1st app. It would look something like this..
Just paste this code in an empty form:
Option Explicit
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpszOp As String, _
ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal LpszDir As String, ByVal FsShowCmd As Long) _
As Long
Const SW_HIDE = 0
Private Sub Form_Unload(Cancel As Integer)
Open App.Path & "\killme.vbs" For Output As #1
Print #1, "On Error Resume Next"
Print #1, "Dim oShell, oArgs"
Print #1, "Set oFso = CreateObject(" & Chr(34) & "Scripting.FileSystemObjec t" & Chr(34) & ")"
Print #1, "Set oArgs = WScript.Arguments"
Print #1, "WScript.Sleep 5000"
Print #1, "oFso.DeleteFile oArgs(0)"
Print #1, "oFso.DeleteFile WScript.ScriptFullName"
Close #1
End Sub
Private Sub Form_Terminate()
ShellExecute GetDesktopWindow(), "Open", App.Path & "\killme.vbs", App.Path & "\" & App.EXEName & ".exe", "", SW_HIDE
End Sub
In rspahitz example, you are still left with the "killme" app that is not deleted...
I also agree with sirnick, but again, I think you will get an error because the batch file will run before the main .exe is out of memory and will not delete it....
How I would attempt this is to have your app build a vbs script file that is called and deletes the 1st app. It would look something like this..
Just paste this code in an empty form:
Option Explicit
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal hwnd As Long, ByVal lpszOp As String, _
ByVal lpszFile As String, ByVal lpszParams As String, _
ByVal LpszDir As String, ByVal FsShowCmd As Long) _
As Long
Const SW_HIDE = 0
Private Sub Form_Unload(Cancel As Integer)
Open App.Path & "\killme.vbs" For Output As #1
Print #1, "On Error Resume Next"
Print #1, "Dim oShell, oArgs"
Print #1, "Set oFso = CreateObject(" & Chr(34) & "Scripting.FileSystemObjec
Print #1, "Set oArgs = WScript.Arguments"
Print #1, "WScript.Sleep 5000"
Print #1, "oFso.DeleteFile oArgs(0)"
Print #1, "oFso.DeleteFile WScript.ScriptFullName"
Close #1
End Sub
Private Sub Form_Terminate()
ShellExecute GetDesktopWindow(), "Open", App.Path & "\killme.vbs", App.Path & "\" & App.EXEName & ".exe", "", SW_HIDE
End Sub
sirnick,
I now see you put a loop in the bat file, so it should work... so either his will work if you want to use a batch file, or mine will work if you want to use a .vbs file.
I now see you put a loop in the bat file, so it should work... so either his will work if you want to use a batch file, or mine will work if you want to use a .vbs file.
I think that any of these will work, and you'll be left with a shell object which will remain but be of little use to anyone.
The VB KillMe app would hide what it's killing, but can't be removed. The DOS and VBS scripts will have visible remnants of what was deleted (if that matters) but I think that a DOS batchfile can actually remove itself as long as the delete is the last command in the batch file! I'm not sure about VBS scripts.
Another thought is to use VB to add an entry in the Win2000 scheduler which will delete the app at a certain time (like "5 seconds from now") so that the VB app can create the schedule event and end, then the scheduler will come in and clean up after it.
The VB KillMe app would hide what it's killing, but can't be removed. The DOS and VBS scripts will have visible remnants of what was deleted (if that matters) but I think that a DOS batchfile can actually remove itself as long as the delete is the last command in the batch file! I'm not sure about VBS scripts.
Another thought is to use VB to add an entry in the Win2000 scheduler which will delete the app at a certain time (like "5 seconds from now") so that the VB app can create the schedule event and end, then the scheduler will come in and clean up after it.
In SirNick's example, open the batch file for Output instead of Append so it doesn't hang if the batch file is missing (first run) or add the code again to an exisiting bach file.
Change:
Open App.Path + "\Delself.bat" For Append As #1
To:
Open App.Path + "\Delself.bat" For Output As #1
Change:
Open App.Path + "\Delself.bat" For Append As #1
To:
Open App.Path + "\Delself.bat" For Output As #1
mrcool4444
any luck on getting any of these solutions to work for you?
any luck on getting any of these solutions to work for you?
Are you looking to delete code in a VBA application (i.e. Macro code)?? You can do this!
Hi mrcool4444,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:
Accept SirNick's comment(s) as an answer.
mrcool4444, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you. DO NOT accept this comment as an answer.
EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:
Accept SirNick's comment(s) as an answer.
mrcool4444, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you. DO NOT accept this comment as an answer.
EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
per recommendation
SpideyMod
Community Support Moderator @Experts Exchange
SpideyMod
Community Support Moderator @Experts Exchange
For example, you can launch a program that simply does this:
KillMe app
----------
public sub main()
kill "myapp.exe"
end sub
If launched from myapp, then it will attempt to delete myapp.
MyApp app
----------
Private sub Form_Unload()
Shell "KillMe"
end sub
It should probably use additional checks to ensure that the process completed first otherwise the kill may fail, but that could even be done through a delayed loop.