Remove file or directory in a batch file

Posted on 24-04-2013 08:19 by graham
If you want to remove a file or a directory in a batch file, use the rd command, which removes a path, no matter if it's a regular file or a directory.

An example of such batch file would be:
rd /s c:\some\directory
rd /s c:\some\regular\file.txt

The /s option tells the command to delete a directory with all subdirectories and files - the whole directory tree.

The script above, however, will ask you for confirmation every time it deletes a file. To run it in quiet mode (with no user interaction), use the /q switch:
rd /s /q c:\some\directory
rd /s /q c:\some\regular\file.txt


Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems