Search code examples
windowsbatch-file

Deleting an Exe Batch Script itself


I have compiled my batch (.bat) file into exe, lets name it Run.exe, I want to tell it to delete itself at the end of its work. How can I do this through commands?


Solution

  • Put this at the end of your script:

    ( del /q /f "%~f0" >nul 2>&1 & exit /b 0  )
    

    This will tell it to exit and delete its self and i have tested it when compiled to an exe so it works. This can be a bit unreliable so you could try this also

    call :end&exit /b
    :end
    start /b "" cmd /c del "%~f0"&exit /b
    

    Try both and see witch one works best for you.