RD - CMD


Overview

The RD (Remove Directory) command in Windows CMD is used to delete directories (also referred to as folders). This command is particularly useful for cleaning up empty or unwanted directories in a filesystem. It is commonly employed in scripting and batch operations to maintain organizational structure or prepare environments.

Syntax

The RD command follows this basic syntax:

RD [path] [options]

Where [path] is the directory to be removed. If the path contains spaces, it must be enclosed in quotes.

Options/Flags

  • /S : Removes all directories and files in the specified directory in addition to the directory itself. Used to delete whole directory trees.

  • /Q : Quiet mode, does not ask for confirmation when deleting a directory tree with /S.

Default behavior is to delete only empty directories. If a directory is not empty and the /S flag is not specified, the command will not execute and an error will be displayed.

Examples

  1. Delete an empty directory:

    RD "C:\example"
    

    This command deletes the directory named example located at C:\, but only if it is empty.

  2. Delete a directory and its contents:

    RD /S /Q "C:\example"
    

    Deletes the directory example along with all its subdirectories and files without asking for confirmation.

Common Issues

  • Directory not empty: Attempting to delete a non-empty directory without using /S will result in an error. Always ensure directories are empty or use /S to force deletion.
  • Permission issues: Lack of sufficient permissions can prevent directories from being deleted. Run CMD as an administrator if permissions errors persist.

Integration

RD can be combined with other CMD commands for powerful scripts. For instance, using DIR to list directories followed by conditional deletion:

FOR /D %p IN ("C:\temp\*.*") DO RD /S /Q "%p"

This script will delete all directories under C:\temp.

  • DEL : Used for deleting files within a directory.
  • MD or MKDIR: Commands to create a new directory.
  • CD or CHDIR: Change the current directory.

For more advanced file and directory manipulations, exploring these commands will prove beneficial. Official Microsoft documentation provides a comprehensive resource for deeper understanding and additional parameters.

For further reading and resources, the official Microsoft command-line reference is a valuable tool.