I'm looking for a way to find and remove every folder which contains files with extension .drt in it.

It's a networkshare and structure is shown below, in this case I want to delete subfolder 1 and 3


Basically, you first search for matching files, extract their path and then delete the path. This will work:

for /f "delims=" %%F in ('dir /b /s *.drt') do @if exist %%F rd /s /q %%~dpF

The search command is just dir, the list is processed in a FOR loop, and the path extraction is done via %%~dp, i.e. only drive and path is extracted from each file found.
Note that removing the folder is only attempted after testing to avoid superfluous error messages.

  • will report "path not found" - Add 2> nul to throw way the errors ...
    – DavidPostill
    Jul 26 '16 at 13:30
  • Added code to avoid these messages but to keep real ones, e.g. permission denied. +1 for muting the command part with @. Jul 26 '16 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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