Self Deleting Script

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Automation Wizard
Posts: 3396
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Self Deleting Script

Post by JRL » Fri May 22, 2020 4:42 pm

I needed an executable that would run and then remove itself. I knew I'd posted something like that before. Turns out it was 15 years ago and uses old dialogs so I rewrote an example to post and used a stock message box to present information.

A couple of things.
- Don't run this from the editor, the script has to have a name on the file system to be deleted,
- There is an Exit>0 line 9 lines down from the top. It's there to prevent the sample from deleting itself. It needs to be remarked or removed for the script to delete itself.
- After successfully running and deleting the script, the script name will still display in the main menu. You'll need to manually delete the entry.
- My intent was to have an executable that would delete itself. This script will work compiled.

Much of the script is example foolishness. The seven lines that are important are where the script creates a batch file that is set up to delete the script (four lines). The script then runs the batch file and exits so that the script is no longer an active process (three lines). This allows the batch file to delete the script. The batch file then deletes itself. I suppose a batch file can delete itself because its running in a command shell process rather than in its own process?

Code: Select all

//Check to make sure there is a filename to work with
  MDL>You have not given your script a name.  Can't self destruct a nameless script.

//Safety net for keeping a copy that won't self destruct.
//Remark or remove this exit line to self destruct this script.

//Create the batch file that deletes the script then itself.
WriteLn>%Temp_dir%SelfDestruct.bat,wres,Timeout 1
WriteLn>%Temp_dir%SelfDestruct.bat,wres,Del "%script_File%"
WriteLn>%Temp_dir%SelfDestruct.bat,wres,Timeout 1
WriteLn>%Temp_dir%SelfDestruct.bat,wres,del "%Temp_dir%SelfDestruct.bat"

//Set the run program options to not wait for the program to complete.

//Move the message box to the top of the screen
//Change the message box name
GetWindowHandle>Macro Scheduler Message,vWinHandle
LibFunc>user32,SetWindowTextA,res,vWinHandle,Good Luck %user_name%

//Count down the time in the message box
  SetControlText>Good Luck %user_name%,TMemo,1,As always, should you or any of your MS Force be detected or inspected, the forum will disavow any knowledge of your behavior (behaviour).  This file will self destruct in %vTimeout% seconds.

SetControlText>Good Luck %user_name%,TMemo,1,%crlf%%crlf%%tab%P f f f f f f f f f f f f f f f f t
CloseWindow>Good Luck %user_name%

//Run the self destruct batch file and the script ends

Posts: 8
Joined: Fri May 01, 2020 10:06 am

Re: Self Deleting Script

Post by geon12 » Wed Sep 02, 2020 7:11 am

I know this post is from long time ago- but this fixed the problem I've been stuck on forever!! Thanks!!

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