28

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

D:\Air
D:\Abonden
D:\All
D:\Whatever

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?

  • Uniformity and common sense were never at play in the development of the Windows or DOS command shells. The sad part is there were model systems out there when these shells were developed that would have shed light on intelligent designs. Of course the shell is just one example of the horrible to non-existent thought process that went into slapping together this garbage -- I wish I could tell you what I really think, ha ha. – Rick O'Shea Jan 17 '17 at 17:10
28
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.

  • As-is, the command will break if the path/folder name contains space characters. – and31415 Jun 6 '14 at 8:26
  • 1
    Can we add "IF NOT EXIST goto EFO" for A*? – serdar Jun 6 '14 at 8:39
  • 2
    This works if the path containes space chars: for /f "delims=" %i in ('dir /a:d /s /b A*') do rd /s /q "%i" – Luis Cantero - MSFT May 20 '16 at 12:14
  • 4
    I would just like to add that if running this from within a batch file then you need to use two percent signs so the command becomes for /f %%i in ('dir /a:d /s /b A*') do rd /s /q %%i – Fred Clausen Jun 22 '16 at 2:49
  • no need to use flag 'w/sub' twice :) – lunicon Aug 23 '16 at 7:42
20

Deleting folders using wildcards

The rmdir / rd command alone doesn't support wildcard characters (that is, * and ?). You can workaround this limitation by wrapping it in a for loop.

Example usage

for /d %G in ("X:\A*") do rd /s /q "%~G"

Note As you're deleting files and folders, you might want to replace the rd command with echo first. This way you can ensure anything that shouldn't be deleted actually would.

Multiple patterns

In order to delete multiple folders matching different patterns the syntax is not too different. As @dbenham correctly pointed out, a one-line command is enough. You can also specify different paths:

for /d %G in ("X:\A*","Y:\Whatever\B*","Z:\C?D") do rd /s /q "%~G"

Bonus - Checking folder existence

In case you want to check whether specific folders exist, you can use the following command:

dir /b /a:d "X:\A*" >nul 2>&1 && echo Folders exist. || echo No folders found.

Further reading

  • Can we add "IF NOT EXIST goto EFO" for A*? – serdar Jun 6 '14 at 8:39
  • For example we suppose there is no folder starting with A letter. So command must go for B* . I dont know if I could explain or not :( – serdar Jun 6 '14 at 10:53
  • 1
    +1 from me. I don't understand the down vote. Though you can do multiple patterns with your simple FOR /D and FOR /D /R commands. Simply use in( "A*" "B*" "C?D" ). – dbenham Jun 7 '14 at 4:36
  • 1
    Actually you do need the /R option because the subfolder that matches the mask might be under a parent that does not. – dbenham Jun 7 '14 at 11:27
  • 4
    WARNING: If you want to use this solution in a .bat file, you have to use %%G instead of %G, see here – ValarDohaeris Jul 29 '15 at 13:02
15

How has nobody told the OP about forfiles yet?!

forfiles /P D:\ /M A* /C "cmd /c if @isdir==TRUE rmdir /s /q @file"

/P is pathname - where the searching starts
/M is search mask, looking for files that start with A
/C is the command to execute
/S is recursive subfolders (didn't include here, because op didn't ask)
  • 2
    This is an excellent answer, should be the accepted one. – eacousineau Nov 30 '15 at 15:04
  • 1
    Very good solution and well explained. – mrt Dec 17 '15 at 11:34
  • Useful answer :D but IMO for /d as in and31415's answer is both cleaner and more efficient. – Superole Nov 10 '16 at 11:29
7

adn31415 answer is correct but breaks if you put this in a batch or cmd script. I banged my head for hours till I figured out this is how you use it.

In DOS command window:

for /d %G in ("X:\A*") do rd /s /q "%~G"

In Batch or cmd script:

for /d %%G in ("X:\A*") do rd /s /q "%%~G"

If you want to put it into a ".bat" or a ".cmd" file, you need to double the "%" characters.

  • You saved my time for the the Batch script! – Tuan Oct 19 '15 at 3:58
  • no problem dude :) That's why posted the answer as I knew it wasn't obvious and more people are going to run into it. – Sam B Oct 19 '15 at 13:23
  • Can we add an exclude not to be deleted? – serdar Feb 17 '16 at 20:16
  • SET /P PC=ENTER IP OR HOST NAME for /d %%a in ("\\%PC%\D$\A*.*") do rd /s /q "%%a" Does this scritpt work properly? – serdar Feb 17 '16 at 20:17

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.