I need to delete all sub-folders in a folder tree that match the pattern "images".
I can remove all files matching a pattern using
del /s myfolder\images*.*
but it doesn't work for directories.
I tried with rmdir too but without success
rmdir /s
may be what you want to purge a sub-tree, and, as Floris said, this is very dangerous. If I understand your question correctly, you want to be able to delete myfolder\a\b\c\holiday_images
while leaving myfolder\a\b\c\holiday_videos
intact.
This should do what you want:
for /d /r %i in (*image*) do @rmdir /s "%i"
This is native Windows functionality; no need to download or install anything.
A bit of general advice for running potentially dangerous commands like this is to do
for /d /r %i in (*image*) do @echo rmdir /s "%i"first; that will show you what it will do, but not do anything. After you verify that it’s doing what you want, run it again without the
echo
.
If you want to use this command in a batch file (script), you must double the percent characters; i.e.,
for /d /r %%i in (*image*) do @rmdir /s "%%i"
for /d /r %%i in (DIV_sv*) do ( @rmdir /s /q "%%i" echo Removed "%%i" )
First paren must be on same line as do right after do, and rmdir command must be on next line.
@
before the (
, and (2) You need either a &
or an (Enter) before the echo
. Also, as you have noticed, comments don’t allow you to display multi-line commands well. It helps if you enter separate lines as separate code strings; for example, `line 1` / `line 2` / `line 3` displays as line 1
/ line 2
/ line 3
.
Jan 23, 2020 at 20:41
DIV_sv*
) than the one in the question?
Jan 23, 2020 at 20:50
Here's how to show each dir being deleted as it's deleted.
for /d /r %%i in (DIV_sv*) do (
@rmdir /s /q "%%i"
echo Removed "%%i"
)
First paren must be on same line as do right after do, and rmdir command must be on next line.
rm -rf myfolder/images will delete without asking otherwise you can go -ri to confirm every deletion.
rmdir -r
works in my OS - but you did tag as Windows... The-r
flag means "recursively". To force deletion you addf
, sormdir -rf
. VERY dangerous command...