9

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

1
  • 1
    rmdir -r works in my OS - but you did tag as Windows... The -r flag means "recursively". To force deletion you add f, so rmdir -rf. VERY dangerous command...
    – Floris
    Feb 4, 2013 at 4:29

3 Answers 3

21

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"
5
  • @EricL: Thanks for catching that. I know to use quotes in shell commands (and, in fact, I’m continually telling people to do it), but I keep forgetting to do it in CMD. Jan 29, 2018 at 18:02
  • 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.
    – Bulrush
    Jan 22, 2020 at 14:35
  • @Bulrush: Yes, but (1) It works better if you put @ 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
  • Also, why did you use a different pattern (DIV_sv*)  than the one in the question? Jan 23, 2020 at 20:50
  • @Scott-СлаваУкраїні Kudos for pointing out '@echo'
    – BHP
    Nov 29, 2023 at 7:09
0

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.

-1

rm -rf myfolder/images will delete without asking otherwise you can go -ri to confirm every deletion.

2
  • Ok i have "rm" from GnuWin32 package. However it won't delete the subfolder "myfolder\subfolder1\images". It only removes folders named "myfolder\images1", "myfolder\images2", etc (using asterisk: "rm -rf myfolder\images*")
    – BearCode
    Feb 4, 2013 at 5:37
  • rm -rf myfolder/images/* will remove all files in images, but rm -rf myfolder/images should remove that folder and all of its contents
    – Jarealist
    Feb 4, 2013 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .