To remove all folders in the current directory using pattern matching

I need to delete all folders in the current directory that starts with the name "foo" followed by the date | eg,

  • foo20120620
  • foo20120513
  • fooblabla

I can successfully delete one folder corresponding to the current date
 Example

set FOO_FOLDER=%CD%\foo%datetimef%      

echo Y | rd /s/q %FOO_FOLDER%     

      

But I cannot delete all folders starting with foo.

I have tried something like

set OLD_PATTERN="%CD%\foo"     
del %OLD_PATTERN%*

      

I have googled and tried to follow some of the questions already asked in this forum but it doesn't help me much.

Any suggestion would be very helpful to me.
should be in batch script on the window side.

+1


source to share


3 answers


You can use the command FOR /D

.



for /d %%p in (foo*) do rd /s /q "%%p"

      

+11


source


Magic for a team in dos batch ....

from the command line

for /f "tokens=*" %f in ('dir .\foo* /ad/b') do rd "%f" /s/q 

      

from batch file



for /f "tokens=*" %%f in ('dir .\foo* /ad/b') do rd "%%f" /s/q 

      

/ f runs the command in parentheses and tokenizes it. By saying tokens = * all filename / dir goes into one% f variable

here is an example

C:\temp>md foo3

C:\temp>md foo2

C:\temp>md foo1

C:\temp>dir
 Volume in drive C is TEST
 Volume Serial Number is F47F-AAE1

 Directory of C:\temp

18/06/2012  09:42 p.m.    <DIR>          .
18/06/2012  09:42 p.m.    <DIR>          ..
18/06/2012  09:42 p.m.    <DIR>          foo1
18/06/2012  09:42 p.m.    <DIR>          foo2
18/06/2012  09:42 p.m.    <DIR>          foo3
               0 File(s)              0 bytes
               5 Dir(s)  131,009,933,312 bytes free

C:\temp>for /f "tokens=*" %f in ('dir .\foo* /ad/b') do rd "%f" /s/q

C:\temp>rd "foo1" /s/q

C:\temp>rd "foo2" /s/q

C:\temp>rd "foo3" /s/q

C:\temp>dir /ad
 Volume in drive C is TEST
 Volume Serial Number is F47F-AAE1

 Directory of C:\temp

18/06/2012  09:42 p.m.    <DIR>          .
18/06/2012  09:42 p.m.    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  131,009,933,312 bytes free

C:\temp>

      

+1


source


I know this is old, but I thought it was worth mentioning that the FOR command creates parameter variables that are denoted by a letter, not a number (for example, %% G), so to avoid confusion between parameters and path format letters, avoid using letters (a, d, f, n, p, s, t, x, z) as FOR parameters, or just select the FOR parameter letter that is an UPPER attachment.

The formatting letters are case sensitive, so using a capital letter is also a good way to avoid conflicts, for example. %% A, not %% a.

Link

0


source







All Articles