How to make .BAT file delete it self after completion? I have a simple bat file that terminates a process. I want that .BAT file to delete itself
You can't. The batch file is locked by the operating system during its execution.
You didn't mention the OS, but if this is on Windows XP Professional and you have the appropriate permissions, you can have the batch file schedule a one-shot Windows Scheduled Task to delete the file at a later time. Use the schtasks
command, documented here.
Otherwise, you typically can't delete a file that is being executed, since that has the potential for all sorts of nastiness. Additionally, trying to delete an executable in use is viewed as very suspicious behavior by any number of antivirus programs, so it's likely that you would run afoul of these as well.
@ECHO OFF
SETLOCAL
SET someOtherProgram=SomeOtherProgram.exe
TASKKILL /IM "%someOtherProgram%"
DEL "%~f0"
Note that the DEL line better be the last thing you intend to execute inside the batch file, otherwise you're out of luck :)
[Edit: Missed the "killing other process" part - my batch file originally launched a process]
@Robert Harvey I agree. Just like you can't delete a file when it is in use by a program, you can't have a batch file delete itself while it is running.