Can an exe be made to delete itself?
Posted: Sat Jan 19, 2008 6:37 am
Is there any way an exe can load itself into memory and delete the executable that ran it? I don't think this is possible but I thought I'd ask. 

http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
; Delete running exe, by PB. Works under all versions of Windows.
MessageBox_(0,"This exe will self-destruct when you click OK","Test",0)
RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)
Code: Select all
Procedure DeleteMyself()
scriptname$ = "UNINST"
extension$ = ".vbs"
append=0
testfilename$ = scriptname$+RSet(Str(append),2,"0")+extension$
While FileSize(testfilename$) <> -1
append+1
testfilename$ = scriptname$+RSet(Str(append),2,"0")+extension$
Wend
If CreateFile(0, testfilename$)
WriteStringN(0, "' Wait for calling exe to close, then delete it ")
WriteStringN(0, "Wscript.Sleep 1000")
WriteStringN(0, "Dim fso, Filename")
WriteStringN(0, "Set fso = CreateObject("+Chr(34)+"Scripting.FileSystemObject"+Chr(34)+")")
WriteStringN(0, "Set Filename = fso.GetFile("+Chr(34)+ProgramFilename()+Chr(34)+")")
WriteStringN(0, "Filename.Delete")
WriteStringN(0, "' ")
WriteStringN(0, "' Now delete this script ")
WriteStringN(0, "Dim objFSO")
WriteStringN(0, "Set objFSO = CreateObject("+Chr(34)+"Scripting.FileSystemObject"+Chr(34)+")")
WriteStringN(0, "objFSO.DeleteFile WScript.ScriptFullName")
WriteStringN(0, "Set objFSO = Nothing")
CloseFile(0)
RunProgram(testfilename$)
End
EndIf
ProcedureReturn 0
EndProcedure
DeleteMyself()
Can anyone confirm if this works on Vista.PB wrote:Code: Select all
; Delete running exe, by PB. Works under all versions of Windows. MessageBox_(0,"This exe will self-destruct when you click OK","Test",0) RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)
Works here. Windows 7, 32bwallgod wrote:This does not work on 7.
This is not reliable on any os. It relies on pure timing luck.PB wrote:Code: Select all
; Delete running exe, by PB. Works under all versions of Windows. MessageBox_(0,"This exe will self-destruct when you click OK","Test",0) RunProgram(GetEnvironmentVariable("comspec"),"/c del "+Chr(34)+ProgramFilename()+Chr(34),"",2)
I guess if cmd has a high priority (for any reason), the call could be executed before the program ends (like it's actually the case)netmaestro wrote:Trond, could you elaborate please? How does timing luck affect it? Since PB posted that quite some time ago I've used it and never saw it fail.
Code: Select all
t(0) call cmd
t(1) delete End <- theoretically simultanious