How to make a batch file delete itself?

前端 未结 9 872
遇见更好的自我
遇见更好的自我 2020-11-28 06:07

Is it possible to make a batch file delete itself?

I have tried to make it execute another file to delete it but this did not work.

Does any one know how I cou

相关标签:
9条回答
  • 2020-11-28 06:22
    del %0
    

    As the last line of the file is the easiest way I've found. %0 returns the name of the currently executing .cmd file.

    When I first tried it, I thought I would get a "file in use" error, but that hasn't happened so far.

    0 讨论(0)
  • 2020-11-28 06:27

    In case someone else was trying to use user6811411's 3 run and then self destruct script and it was not working. I figured out the &'s were not being interpreted right. My solution was just to separate the commands like below.

    @Echo off
    Rem batch main purpose
    Rem
    Rem
    Set Tok=###
    For /f %%C in ('find /C "%Tok%" ^<"%~f0"') Do Set /A Cnt=%%C
    Echo Run-%Cnt%
    If %Cnt% Geq 3 (goto) 2>nul & del "%~f0"
    Echo %Tok%>>"%~f0" & Goto :Eof
    
    0 讨论(0)
  • 2020-11-28 06:32

    you can set the delay to avoid race conditions, here is 15 seconds :

    START /B CMD.EXE /D /C "PING.EXE -n 15 127.0.0.1 && DEL prova.bat"
    

    NOTES

    • CMD.EXE /D is mandatory to avoid execution of auto-run crap-ware
    • PING and DEL must be executed syncronous, so surrounded by quotes
    • I let to the users how to pass the batch name using "%~f0"
    0 讨论(0)
提交回复
热议问题