Home » How to Delete Directories Using Wildcards in CMD

How to Delete Directories Using Wildcards in CMD

You can use the following syntax in Command Prompt (CMD) to delete directories using wildcards:

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

This particular example will delete all directories that match the wildcard pattern in the specified path.

Note: The /S flag specifies to remove 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 Delete Directories Using Wildcards in CMD

Suppose that we want to delete all directories that start with temp_ in the following path:

C:\Users\admin\Documents\cmd_script

We can type the following command to delete the directories:

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

Output: 👇️

CMD - delete directories using wildcard
CMD – delete directories using wildcard

We can see that all directories starting with temp_ in C:\Users\admin\Documents\cmd_script are deleted 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 how to delete directories using wildcards in CMD is helpful to you.

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