54

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?

0

4 Answers 4

44
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.

Edit: Please note the comment below from @Friedrich 'Fred' Clausen, which is utterly important, otherwise you may end up deleting whole unwanted directories:

If this is running from within a batch file (which is tagged by this question) you need to use the two percent signs:

for /f %%i in ('dir /a:d /s /b A*') do rd /s /q %%i
6
  • 1
    As-is, the command will break if the path/folder name contains space characters.
    – and31415
    Commented Jun 6, 2014 at 8:26
  • 1
    Can we add "IF NOT EXIST goto EFO" for A*?
    – serdar
    Commented Jun 6, 2014 at 8:39
  • 3
    This works if the path containes space chars: for /f "delims=" %i in ('dir /a:d /s /b A*') do rd /s /q "%i" Commented May 20, 2016 at 12:14
  • 6
    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 Commented Jun 22, 2016 at 2:49
  • 1
    The /s option in the embedded dir command is redundant, since rd /s will remove sub-directories of the A* directories. However, for /d %i in ("A*") do rd /s /q "%~i" - as described in the answer below - is more elegant, IMHO.
    – Mike Allen
    Commented Mar 1, 2017 at 22:54
40

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

16
  • Can we add "IF NOT EXIST goto EFO" for A*?
    – serdar
    Commented Jun 6, 2014 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
    Commented Jun 6, 2014 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
    Commented Jun 7, 2014 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
    Commented Jun 7, 2014 at 11:27
  • 6
    WARNING: If you want to use this solution in a .bat file, you have to use %%G instead of %G, see here Commented Jul 29, 2015 at 13:02
25

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)
1
  • 1
    Useful answer :D but IMO for /d as in and31415's answer is both cleaner and more efficient.
    – Superole
    Commented Nov 10, 2016 at 11:29
21

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.

2
  • Can we add an exclude not to be deleted?
    – serdar
    Commented Feb 17, 2016 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
    Commented Feb 17, 2016 at 20:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .