Self-Deleting EXE File

Share your advanced PureBasic knowledge/code with the community.
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Self-Deleting EXE File

Post 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
Last edited by Randy Walker on Mon Sep 01, 2025 6:24 pm, edited 3 times in total.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4167
Joined: Thu Apr 18, 2019 8:17 am

Re: Self-Deleting EXE File

Post 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. ;)
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Self-Deleting EXE File

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
BarryG
Addict
Addict
Posts: 4167
Joined: Thu Apr 18, 2019 8:17 am

Re: Self-Deleting EXE File

Post by BarryG »

One discussion from 2007 -> viewtopic.php?t=27413 ;)
User avatar
Kiffi
Addict
Addict
Posts: 1496
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Self-Deleting EXE File

Post by Kiffi »

@Randy:

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

[...]

// Edit: Code is here
Hygge
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Self-Deleting EXE File

Post 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.
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Axolotl
Addict
Addict
Posts: 832
Joined: Wed Dec 31, 2008 3:36 pm

Re: Self-Deleting EXE File

Post 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.)
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Self-Deleting EXE File

Post 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!
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
Randy Walker
Addict
Addict
Posts: 1058
Joined: Sun Jul 25, 2004 4:21 pm
Location: USoA

Re: Self-Deleting EXE File

Post 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$
- - - - - - - - - - - - - - - -
Randy
I *never* claimed to be a programmer.
User avatar
Kiffi
Addict
Addict
Posts: 1496
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Self-Deleting EXE File

Post by Kiffi »

Randy Walker wrote: Mon Sep 01, 2025 6:28 pmHi Kiffi ... Could you edit your post [...]
[X] Done
Hygge
Post Reply