

I've discovered another really slick way to have a batch script delete itself without generating any error message. It takes time for the process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens.

Scheduling a task can work, but there is a simpler way: use START to launch a new delete process within the same console. John Faminella has the right idea that another process is needed to cleanly delete the batch file without error. This line contains two commands on one line: DEL for deleting the batch file and. It takes time for the delete process to initiate and execute, so the parent script has a chance to terminate cleanly before the delete happens. Use following command line as last line in your batch file: del 'f0' & exit. This can easily be done using START /B to launch a delete process. But the error message is very undesirable if the console remains open after script termination. The trick to deleting the file without an error message is to get another hidden process to delete the file after the script terminates. The Merlyn Morgan-Graham answer manages to delete the running batch script, but it generates the following error message: " The batch file cannot be found." This is not a problem if the console window closes when the script terminates, as the message will flash by so fast that no one will see it.
