Home » How to Search for a Directory and Delete It in CMD

How to Search for a Directory and Delete It in CMD

You can use the following syntax in Command Prompt (CMD) to search for a directory by name and delete it:

for /D %G in ("directory_path\directory_name_pattern") do rmdir /S /Q "%G"

This particular example will search for directories that match the specified pattern within the given path and delete them.

Note: The /S flag specifies removing all directories and files in the specified directory, and the /Q flag specifies quiet mode, which suppresses confirmation prompts.

The following example shows how to use this syntax in practice.

Example: How to Search for a Directory and Delete It in CMD

Suppose that we want to search for directories that start with temp_ within the following path and delete them:

C:\Users\admin\Documents\cmd_script

We can type the following command to search for and delete the directories:

for /D %G in ("C:\Users\admin\Documents\cmd_script\temp_*") do rmdir /S /Q "%G"

Output: 👇️

CMD - Search for directory and delete it
CMD – Search for the directory and delete it

In the above example, the command searches for directories starting with temp_ within C:\Users\admin\Documents\cmd_script and deletes them without prompting for confirmation.

Note: Use this command with caution as it will permanently delete all matching directories and their contents.

Conclusion

I hope the above article on searching a directory and deleting it in CMD is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.