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:
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.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist.
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.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist.
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.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist.
Are you sure that your batch file is correct?
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).
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.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist.
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.
Nothing is faster, more stable, or easier to maintain than code that doesn’t exist.