Consegui fazer isso com algum código de lote, mas é bastante longo:
setlocal enabledelayedexpansion
:: Delete files not containing "foo"
for /r MyDir %%i in (*) do (
set temp=%%~nxi
if "!temp:foo=!"=="!temp!" (
del "%%i"
)
)
:: Build recursive list of dirs, sorted by deepest first
set dirs=
for /r MyDir /d %%i in (*) do (
set dirs="%%i" !dirs!
)
:: Delete dirs. Nonempty dirs will be skipped.
for %%i in (%dirs%) do (
rmdir %%i
)