Windows – Using wildcards with the rmdir or rd command

batchcommand linewindows

Let's say there are some folder in the D: drive:


I want to delete all folders starting with "A" (including all subfolders and files). I tried this command:

rmdir D:\A* /s /q

I get an error, though 🙁

The filename, directory name, or volume label syntax is incorrect.

The del command works with *, but I need to delete folders as well.
Is there a way to achieve that via the rmdir command?

Best Answer

cd c:\temp
for /f %i in ('dir /a:d /s /b A*') do rd /s /q %i

Use this to test though:

for /f %i in ('dir /a:d /s /b A*') do echo rd /s /q %i

This will pipe out the commands to be run into the command prompt and allows you to see what's going on.

Bear in mind that this will also search subfolders such as "C:\temp\jjj\aaa" and would delete the aaa folder. If you want it to just look at top level folders "C:\temp\aaa", then remove the "/s" from the command.

The key to this is the A*, where you would put in your search string. This will accept wildcards such as aaa*, aaa* and *aaa* if you want it to.