Page 1 of 1

Self-Deleting EXE File

Posted: Sat Aug 30, 2025 3:14 am
by Randy Walker
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:

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()
[EDIT] Slightly revised to make it a procedure

Re: Self-Deleting EXE File

Posted: Sat Aug 30, 2025 3:19 am
by BarryG
Randy Walker wrote: Sat Aug 30, 2025 3:14 amMaybe someone with better knowledge could purify it a bit.
There's lots of posts on how to do this. Just search. ;)

Re: Self-Deleting EXE File

Posted: Sat Aug 30, 2025 3:25 am
by Randy Walker
BarryG wrote: Sat Aug 30, 2025 3:19 am There's lots of posts on how to do this. Just search. ;)
I searched and searched, but nothing found.

Re: Self-Deleting EXE File

Posted: Sat Aug 30, 2025 4:15 am
by BarryG
One discussion from 2007 -> viewtopic.php?t=27413 ;)

Re: Self-Deleting EXE File

Posted: Sat Aug 30, 2025 1:51 pm
by Kiffi
@Randy:

Here are a few suggestions for improvement from me (not exhaustive):

[...]

// Edit: Code is here

Re: Self-Deleting EXE File

Posted: Sat Aug 30, 2025 5:39 pm
by Randy Walker
Hi Kiffi... Thanks for reviewing my code. I like the way you cleaned up the bat file creation.
I thought variables inside a procedure were automatically protected so I never bother with it.
The MessageRequester was only there for demonstration purposes and should probably be removed in real practice to prevent the user from thwarting the cleanup.

Re: Self-Deleting EXE File

Posted: Mon Sep 01, 2025 4:59 pm
by Axolotl
Are you sure that your batch file is correct? :oops:
As far as I remember, a label must begin with a colon.
BTW: Batch files are executed line by line.
In the past, there could not be a CRLF after Del <mybatfile>.... (This may be different today.)

Re: Self-Deleting EXE File

Posted: Mon Sep 01, 2025 6:16 pm
by Randy Walker
Axolotl wrote: Mon Sep 01, 2025 4:59 pm As far as I remember, a label must begin with a colon.
That is also how I recall it but grey matter is not what it used to be.
I've tested it several times and the executable worked flawlessly.
Create the executable and try it yourself.
Possible in my case it was fully successful on first pass and never hit the Goto command.
I checked with Meta AI and he confirmed the colon is required. 👍👍
I will add to the OP -- THANKS!

Re: Self-Deleting EXE File

Posted: Mon Sep 01, 2025 6:28 pm
by Randy Walker
Kiffi wrote: Sat Aug 30, 2025 1:51 pm @Randy:

Here are a few suggestions for improvement from me (not exhaustive):

Code: Select all

EnableExplicit; Always use!

Procedure DeleteThisEXE()
  
  Protected TrashDir$, CleanupBat$, CleanupBatString$
  Protected MessageRequesterResult, FileHandle
  
  MessageRequesterResult = MessageRequester("Proceed?", "We will attempt to delete " + ProgramFilename() + " now...", #PB_MessageRequester_YesNo | #PB_MessageRequester_Warning)
  
  If MessageRequesterResult = #PB_MessageRequester_No
    ProcedureReturn
  EndIf
  
  TrashDir$   = GetPathPart(ProgramFilename()) ; don't use GetCurrentDirectory()!
  CleanupBat$ = GetTemporaryDirectory() + "cleanup.bat"
  
  FileHandle = CreateFile(#PB_Any ,CleanupBat$, #PB_Ascii)
  
  If FileHandle
    
    CleanupBatString$ = "cd /D " + Chr(34) + TrashDir$ + Chr(34) + #CRLF$
    CleanupBatString$ + ":Repeat" + #CRLF$
    CleanupBatString$ + "del " + Chr(34) + ProgramFilename() + Chr(34) + #CRLF$
    CleanupBatString$ + "If exist " + Chr(34) + ProgramFilename() + Chr(34) + " Goto Repeat" + #CRLF$
    CleanupBatString$ + "cd /D " + Chr(34) + ".." + Chr(34) + #CRLF$
    CleanupBatString$ + "rmdir " + Chr(34) + TrashDir$ + Chr(34) + #CRLF$
    CleanupBatString$ + "del " + Chr(34) + CleanupBat$ + Chr(34) + #CRLF$
    
    WriteString(FileHandle, CleanupBatString$)
    CloseFile(FileHandle)
    
    If RunProgram(CleanupBat$, "", TrashDir$)
    EndIf
    End
    
  EndIf
  
EndProcedure

DeleteThisEXE()
Hi Kiffi ... Could you edit your post to include the full colon in front of "Repeat", like so:
CleanupBatString$ + ":Repeat" + #CRLF$

Re: Self-Deleting EXE File

Posted: Mon Sep 01, 2025 7:02 pm
by Kiffi
Randy Walker wrote: Mon Sep 01, 2025 6:28 pmHi Kiffi ... Could you edit your post [...]
[X] Done