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
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.
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
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