Self-Deleting EXE File
Posted: Sat Aug 30, 2025 3:14 am
I struggled and hacked at this for most the day trying to get it to work. Maybe someone with better knowledge could purify it a bit. You'll need to create this as an executble and put in an empty folder named C:\Test and when you run it it will vanish all traces of the EXE and the BAT that it created to remove the EXE:
[EDIT] Slightly revised to make it a procedure
Code: Select all
Procedure DeleteThisEXE()
DeleteMe$ = ProgramFilename()
TrashDir$ = GetCurrentDirectory()
buffer$=Space(512)
CleanupBat$=""
MessageRequester("Delete This EXE file.", "We will attempt to delete "+DeleteMe$+" now...", #MB_OK|#MB_ICONWARNING)
Temp$="AppData"
If GetEnvironmentVariable_(Temp$,@buffer$,512)
Debug buffer$
;RemoveString(buffer$,"Roaming") ; NOT WORKING
c=Len(buffer$)
buffer$ = Left(buffer$,c-7)
CleanupBat$ = buffer$+"local\Temp\cleanup.bat"
EndIf
Debug CleanupBat$
;made possible from info learned here: https://www.catch22.net/tuts/system/self-deleting-executables/
If CreateFile(0,CleanupBat$,#PB_Ascii)
s$="cd /D "+Chr(34)+TrashDir$+Chr(34)
WriteStringN(0,s$)
s$=":Repeat"
WriteStringN(0,s$)
s$="del "+Chr(34)+DeleteMe$+Chr(34)
WriteStringN(0,s$)
s$="If exist "+Chr(34)+DeleteMe$+Chr(34)+" Goto Repeat"
WriteStringN(0,s$)
s$="cd /D "+Chr(34)+".."+Chr(34)
WriteStringN(0,s$)
s$="rmdir "+Chr(34)+TrashDir$+Chr(34)
WriteStringN(0,s$)
s$="del "+Chr(34)+CleanupBat$+Chr(34)
WriteStringN(0,s$)
CloseFile(0)
EndIf
If RunProgram(CleanupBat$,"",TrashDir$)
EndIf
End
EndProcedure
DeleteThisEXE()