Recursively delete all folders starting with

24,554

Solution 1

This is the complete answer you are looking for:

FOR /D /R %%X IN (certain_string*) DO RD /S /Q "%%X"

where obviously you need to replace certain_string with the string your folders start with.

This deletes RECURSIVELY as you asked (I mean it goes throught all folders and subfolders).

Solution 2

How about:

for /d %a in (certain_string*) do rd /s %a

This will work from the command prompt. Inside a batch file, you would have to double the %s, as usual:

@echo off
for /d %%a in (certain_string*) do rd /s %%a

Solution 3

Unfinished, I think. If you meant "Recursively go down a directory hierarchy to delete all folders starting with a certain string", then the following might suffice:

for /f "delims=" %%x in ('dir /b /ad abc*') do rd /s /q "%%x"

This will recurse into the directory tree, finding all folders starting with "abc", iterate over that list and removing each folder.

Maybe you need to wrap an if exist around the rd depending on the order in which directories are found and returned. In general, iterating over something and changing it at the same time is rarely a good idea but sometimes it works :-)

Share:
24,554

Related videos on Youtube

glmxndr
Author by

glmxndr

O}=<

Updated on December 11, 2020

Comments

  • glmxndr
    glmxndr over 3 years

    I need to write a command in a .bat file that recursively deletes all the folders starting with a certain string. How may I achieve this ?

  • Rich
    Rich over 14 years
    Ouch. I need more sleep ... or tea ... (hits head on the table)
  • Rich
    Rich over 14 years
    Hmm, although that probably won't recurse into the dir tree and find deeper folders matching the criteria, right? (My solutions don't do that as well, but I just realized that's what the OP meant).
  • Greg Hewgill
    Greg Hewgill over 14 years
    I had to expand this a bit from my original simple attempt, because rd doesn't appear to expand wildcards by itself.
  • Greg Hewgill
    Greg Hewgill over 14 years
    To recursively look for directories starting with a prefix, you may be able to use for /r or some combination thereof.
  • Rich
    Rich over 14 years
    Oh, not nice. I didn't try it either since I currently have no directories lying around to wreck :-)
  • Marco Demaio
    Marco Demaio almost 14 years
    Rössel: did this work? It doesn't work when trying to delete directories ending with ".delme", I tried your suggestion like this: for /f "delims=" %%x in ('dir /b /ad *.delme') do rd /s /q "%%x" BUT IT DOES NOT SEEM TO WORK.
  • Rich
    Rich almost 14 years
    @Marco: No need to shout. If in doubt, ask a new question and tell your problems clearly and in detail. Something like »Doesn't work« is usually a bad issue report.
  • Marco Demaio
    Marco Demaio almost 14 years
    Rossel: wasn't shouting, I just wrote upper case. I'll ask new question then.
  • IsmailS
    IsmailS almost 14 years
    Nothing other than @Maorco's answer worked for me. Sadly he has not got any votes other than mine. :(
  • Marco Demaio
    Marco Demaio about 13 years
    @Greg Hewgill: did this work? It doesn't work when trying to delete directories ending with ".delme", I tried your suggestion like this: for /d %%x in (*.delme) do rd /s /q "%%x" but it does not seem to work.
  • Rob W
    Rob W almost 12 years
    Thx. I had to exclude two directories, which resulted in this command: FOR /D /R %%X IN (*.lproj) DO IF /I "%%~xnX" neq "en.lproj" IF /I "%%~xnX" neq "english.lproj" RD /S /Q "%%~fX". Explanation: %%~xnX equals the full directory name. IF /I .. neq "en.lproj" filters all directories which are equal to "en.lproj" (/I = case-insensitive). IFs can be nested. In the end, I've recursively deleted all directories ending with .lproj, except for en.lproj and english.lproj.