Delete all folders on all drives with "foo" OR "bar" in the folder name with batchfile

Solution 1:

Read and follow FOR - Conditionally perform a command several times.. You could apply either FOR-Folders, or FOR-Command Results as follows:

FOR-Folders (disadvantages: case insensitive; wildcards allow no regex-like pattern so we need to run a loop more times).

@echo off
set "dir=c:\FOLDERLOCATION\"
pushd "%dir%"
::                          ↓↓↓↓                      echo for debugging
FOR /D /R %%X IN (*foo*) DO echo RMDIR /S /Q "%%~fX"
FOR /D /R %%X IN (*bar*) DO echo RMDIR /S /Q "%%~fX"
::                          ↑↑↑↑                      echo for debugging
popd
pause

FOR-Command Results in combination with findstr (advantages: findstr independently allows both case sensitive and regex-like search pattern):

@echo off
set "dir=c:\FOLDERLOCATION\"
FOR /F "delims=" %%X IN ('dir /B/S /A:D "%dir%" ^| findstr /I "foo|bar"') DO 2>NUL echo RMDIR /S /Q "%%~fX"
:: ECHO in above line merely for debugging
pause

A disadvantage is that dir list is generated statically so we need to redirect error messages using 2>NUL