12

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 ?

jdhao
  • 24,001
  • 18
  • 134
  • 273
glmxndr
  • 45,516
  • 29
  • 93
  • 118

5 Answers5

42

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).

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
  • 2
    Nothing other than @Maorco's answer worked for me. Sadly he has not got any votes other than mine. :( – IsmailS Jun 10 '10 at 06:27
  • 1
    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`](http://stackoverflow.com/a/636391) 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](http://stackoverflow.com/a/2143203). In the end, I've recursively deleted all directories ending with `.lproj`, except for `en.lproj` and `english.lproj`. – Rob W Jun 11 '12 at 13:03
4

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
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Ouch. I need more sleep ... or tea ... *(hits head on the table)* – Joey Nov 27 '09 at 08:42
  • 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). – Joey Nov 27 '09 at 08:49
  • I had to expand this a bit from my original simple attempt, because `rd` doesn't appear to expand wildcards by itself. – Greg Hewgill Nov 27 '09 at 08:50
  • To recursively look for directories starting with a prefix, you may be able to use `for /r` or some combination thereof. – Greg Hewgill Nov 27 '09 at 08:51
  • Oh, not nice. I didn't try it either since I currently have no directories lying around to wreck :-) – Joey Nov 27 '09 at 08:52
  • @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. – Marco Demaio Mar 22 '11 at 09:17
1

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 :-)

Joey
  • 344,408
  • 85
  • 689
  • 683
  • 1
    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. – Marco Demaio May 11 '10 at 20:51
  • 2
    @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. – Joey May 11 '10 at 21:46
  • 2
    Rossel: wasn't shouting, I just wrote upper case. I'll ask new question then. – Marco Demaio May 13 '10 at 11:25
0

rm -rf -- "Directory name"

Ex : rm -rf -- "-2096378"

Above command will deletes the folders/directories starting with - or wildcard characters

-1
FOR /F "tokens=*" %i IN ('DIR **[[SearchTerm]]** /A:D /s /b') do rd /S / Q %i
Jens
  • 67,715
  • 15
  • 98
  • 113