4

Is that anyway I can write a batch can delete itself, with exit the command prompt? I have no idea how to do this. If I delete that batch first, then exit cannot run. If I exit first, then I won't able to delete the batch.... Can I do this within the same batch file ? Thanks for any clue!!!

Panda
  • 73
  • 1
  • 6

1 Answers1

6

Use following command line as last line in your batch file:

del "%~f0" & exit

This line contains two commands on one line:

  • DEL for deleting the batch file and
  • EXIT for exiting command process.

The operator & results in execution of second command independent on result of first command. For more details about usage of multiple commands on one line see for example Single line with multiple commands using Windows batch file.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143