How to make .BAT file delete it self after completion?

后端 未结 6 1354
渐次进展
渐次进展 2020-11-29 02:05

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.

6条回答
  •  猫巷女王i
    2020-11-29 02:37

    @ECHO OFF
    SETLOCAL
    
    SET someOtherProgram=SomeOtherProgram.exe
    TASKKILL /IM "%someOtherProgram%"
    
    ECHO "This script will now self-destruct. Please ignore the next error message"
    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 :)

    This will print out an ugly error message, but it is benign, and the code is slightly less confusing this way. If you care a lot about getting rid of the error message, see dbenham's answer to get rid of it.

提交回复
热议问题