# How to delete matching directories recursively using Windows command?

I've a folder `C:\Users\user\Desktop\htmlreports\` and inside there are different folders but I want to delete only folders containing text say `192` in their names like

``````abc192
xyz192
pqr192xyz
``````

apart from these there are also other folders.

To delete only matching folders with text `192`, I tried following -

``````rd /s /q C:\Users\user\Desktop\htmlreports\*192*
``````

But it results into `The filename, directory name, or volume label syntax is incorrect.`

Please suggest what is correct regex which will help to achieve this.

NOTE This is for Windows environment

One line using `cd /D folder && for /Directory /Recursive`

``cd/d "%userprofile%\Desktop\htmlreports" && for /D /R  %i in (*192*)do rmdir /s /q "%~i"``

``cd /D "C:\Users\user\Desktop\htmlreports"``
• Or, using one system variable:
``cd /d "%userprofile%\Desktop\htmlreports"``

2. Use `&&` operator

``cd /D ... && for /D /R... do ...``

Obs.: 1 Only if `cd /D` `return 0`, do the next `for /D /R` command will be executed...

Obs.: 2 You can add `echo\` to test and check the output before deleting any files/folders:

``cd/d "%userprofile%\Desktop\htmlreports" && for /D /R  %i in (*192*)do echo\rmdir /s /q "%~i"``

• Thanks this worked for me! And really appreciate your detailed explanation. Thanks a lot for that :) Aug 25, 2020 at 12:53

Try with cmd (`rd` will not work becuase it only can remove directories, use `del` as below):

``````del /s /q "C:\Users\user\Desktop\htmlreports\*192*"
``````

Try with powershell:

``````dir "C:\Users\user\Desktop\htmlreports" -filter *192* | rm
``````
• Wasif, thanks for the answer but `del` only deletes files but not folders and your powershell command works but it asks for confirmation. `rm -f` with that command did not help to skip prompt. Actually I want to execute this through Jenkins pipeline starting powershell will be addition hence superuser.com/a/1580528/1156098 is perfect solution for me Aug 25, 2020 at 12:44

The PowerShell option:

``````\$Source = 'C:\Users\user\Desktop\htmlreports'

Get-ChildItem \$Source -Directory -Recurse | Where Name -match '192' | Remove-Item -Recurse
``````

Using aliases, we can shorten this to:

``````gci 'C:\Users\user\Desktop\htmlreports' -ad -r | ? Name -match '192' | ri -r
``````