How do I make the exe delete itself?

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
Boxxxed
Junior Coder
Posts: 26
Joined: Fri Mar 19, 2010 7:20 pm

How do I make the exe delete itself?

Post by Boxxxed » Fri Mar 19, 2010 7:23 pm

Is this possible? it has to be!

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Mar 19, 2010 9:48 pm

A script (.SCP file) can delete itself because it is not the running process. That's not the case with an executable. An executable starts a running process and while its process is running can't be deleted.

So what you have to do is somehow end the process and still have a delete command issued by the executable lurking. You can do this by having the executable write then run a batch file that looks like this:

choice /C:~ /T:~,2
Del C:\path\executable.exe

The "2" at the end of the first line is the number of seconds that the batch file will wait until it processes the second line which should delete the specified file.

Be sure that rp_wait is set to 0.

Boxxxed
Junior Coder
Posts: 26
Joined: Fri Mar 19, 2010 7:20 pm

Post by Boxxxed » Sat Mar 20, 2010 12:30 am

How do I make the exe create a batch file?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sat Mar 20, 2010 2:02 am

Boxxxed wrote:How do I make the exe create a batch file?
Lookup the WriteLn> command in the Help File... or here on this website, from the Navigation menu at the top, click Support/Online Help... then click on Command Reference... then click on the WriteLn> command.

Take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Sun Mar 21, 2010 5:00 am

I forgot that choice doesn't come with newer operating systems, I've been copying it upward for years. We can set a batch file delay using the ping command also. The "2" between the "-n" and the "-w" is the number of seconds of delay. The following will compile to "C:\DeleteThisExecutable.exe". The batch file is set up to delete "C:\DeleteThisExecutable.exe". The batch file also deletes itself. Unremark the rp_windowmode line to prevent the DOS window from appearing.

Code: Select all

// COMPILE_OPTS|C:\DeleteThisExecutable.exe||CONSOLE=0|INCLUDES=1|

WriteLn>c:\DeleteMe.bat,wres,PING 0.0.0.0 -n 2 -w 1000 >NUL
WriteLn>c:\DeleteMe.bat,wres,Del C:\DeleteThisExecutable.exe
WriteLn>c:\DeleteMe.bat,wres,Del C:\DeleteMe.bat

//Let>rp_windowmode=0
Let>rp_wait=0
Run>c:\DeleteMe.bat

Boxxxed
Junior Coder
Posts: 26
Joined: Fri Mar 19, 2010 7:20 pm

Post by Boxxxed » Sun Mar 21, 2010 5:38 am

Great, thanks

User avatar
Marcus Tettmar
Site Admin
Posts: 7380
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Mar 22, 2010 9:36 am

Another way would be to create and run a small .vbs file which sleeps for a while and then performs the delete. Or, assuming windows task scheduler is running, create a task to delete in a few mins time.

However, it should go without saying, that self-deletion is a dirty trick and isn't something you should do unless you have a very good reason. Also bear in mind that if the file/folder is marked as read only, or is in a location that the current user doesn't have write access to, the deletion will fail.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

newuser
Pro Scripter
Posts: 64
Joined: Tue Jun 11, 2013 4:53 pm

Post by newuser » Fri Aug 09, 2013 12:43 pm

Not really true, even some version of adobe flashplayer also selfdelete itself after running.

BTW, heres one vbscript modify from a sample found on the internet.

[code]
Set objFSO = CreateObject("Scripting.FileSystemObject")
strScript = Wscript.ScriptFullName
objFSO.DeleteFile(strScript)
Wscript.Sleep 2000
objFSO.DeleteFile(yourexefilename)
[/code]

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Post by CyberCitizen » Fri Aug 09, 2013 2:03 pm

Instead Of Ping

timeout /T 2
FIREFIGHTER

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts