Using wildcards with the rmdir or rd command

40

12

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?

serdar

Posted 2014-06-06T07:44:46.213

Reputation: 515

2Uniformity 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 – 2017-01-17T17:10:20.223

Answers

33

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.

Fazer87

Posted 2014-06-06T07:44:46.213

Reputation: 11 177

3This 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 – 2016-05-20T12:14:46.447

5I 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 – 2016-06-22T02:49:25.953

no need to use flag 'w/sub' twice :) – lunicon – 2016-08-23T07:42:31.770

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 – 2017-03-01T22:54:08.543

As-is, the command will break if the path/folder name contains space characters. – and31415 – 2014-06-06T08:26:36.407

1Can we add "IF NOT EXIST goto EFO" for A*? – serdar – 2014-06-06T08:39:29.400

25

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

and31415

Posted 2014-06-06T07:44:46.213

Reputation: 13 382

5

WARNING: If you want to use this solution in a .bat file, you have to use %%G instead of %G, see here

– ValarDohaeris – 2015-07-29T13:02:42.553

I'm getting ~G" was unexpected at this time. when am running the command for /d %G in ("jmeter_output*") do rd /s /q "%~G" in jenkins pipeline but the command runs fine if I run manually, can someone please help? – RanPaul – 2018-06-22T18:34:11.150

Can we add "IF NOT EXIST goto EFO" for A*? – serdar – 2014-06-06T08:39:11.083

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 – 2014-06-06T10:53:12.420

If you want to delete multiple folders starting with a specific letter, you could use something like this in a batch script: for %%A in (A,B) do for /d /r %%B in ("%%~A*") do rd /s /q "%%~B" – and31415 – 2014-06-06T10:58:09.463

You are right but If there wont be A, we want to know which folder doesnt exist ok? So can we add this? for ex: ===> if not exist "\%PC%\D$\A.*" goto :OTHER <===== like this...., – serdar – 2014-06-06T11:40:53.570

Under :OTHER commmand will work for B*. I said that is why there may be Win Xp or Win 7, changeble directory ok? – serdar – 2014-06-06T11:42:29.827

@serdar Basically you want to delete folders starting with A only if they actually exist; if they don't, folders starting with B should be deleted instead. Is that correct? – and31415 – 2014-06-06T11:55:54.757

Correct totaly, but we want to know which folders werent deleted :) – serdar – 2014-06-06T13:22:25.830

@serdar I've updated my answer, and I've added a way to check whether some specific folders matching a pattern exist. Let me know if you have further questions or doubts. – and31415 – 2014-06-06T16:06:06.827

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 – 2014-06-07T04:36:44.137

@dbenham I totally forgot about using multiple groups like that, overlooking such a simple solution. Thanks! Come to think of it, the executed command is rd /s /q, and the /r parameter isn't required as the subfolders would be deleted anyway. Also, the for /d command allows different paths, unlike the for /d /r variant. I prefer using commas rather than spaces, although parsing-wise it's the same. As for the downvote, while I was writing my answer another one was posted. Possibly whoever did it felt my answer was redundant or lacking in a way. I've since improved and refined it. – and31415 – 2014-06-07T09:13:29.433

1Actually you do need the /R option because the subfolder that matches the mask might be under a parent that does not. – dbenham – 2014-06-07T11:27:42.080

@dbenham Perhaps I took the question too literally, as the intention was to delete only parent folders matching a certain pattern (and all their subfolders), with no specific requirement to handle such cases - in fact, one might not want to. As noted earlier, a possible drawback of the /r option would be the fact you can't handle multiple paths at once. You do make a valid point, though. – and31415 – 2014-06-07T14:38:27.900

'SET /P PC=ENTER IP OR HOST NAME for /d %%a in ("\%PC%\D$\A.") do rd /s /q "%%a"' working very well :) – serdar – 2014-06-07T14:48:24.280

@serdar I'm glad to hear it. If you solved your problem, consider accepting the answer you find the most suitable. – and31415 – 2014-06-07T14:56:24.347

18

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)

JJS

Posted 2014-06-06T07:44:46.213

Reputation: 500

2This is an excellent answer, should be the accepted one. – eacousineau – 2015-11-30T15:04:13.930

1Very good solution and well explained. – Andreas – 2015-12-17T11:34:35.760

Useful answer :D but IMO for /d as in and31415's answer is both cleaner and more efficient.

– Superole – 2016-11-10T11:29:56.580

12

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.

Sam B

Posted 2014-06-06T07:44:46.213

Reputation: 317

3You saved my time for the the Batch script! – Tuan – 2015-10-19T03:58:48.793

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 – 2015-10-19T13:23:23.413

Can we add an exclude not to be deleted? – serdar – 2016-02-17T20:16:40.527

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 – 2016-02-17T20:17:15.217