To delete all folders in the current directory with pattern matching

后端 未结 3 1008
清歌不尽
清歌不尽 2021-01-15 21:07

I need to delete all the folders in current directory that starts with say \"foo\" followed by date
for example we have

  1. foo20120620
  2. foo2
相关标签:
3条回答
  • 2021-01-15 21:44

    The magical for command in dos batch....

    from the command line

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

    from the batch file

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

    /f runs the command in the '' in the brackets, and tokenises its. By saying tokens=* all the file/dir name goe into one variable %f

    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>
    
    0 讨论(0)
  • 2021-01-15 21:49

    I know this is old, but I thought it would be worth mentioning that the FOR command creates parameter variables which are identified with a letter rather than a number (e.g. %%G), so, in order to avoid confusion between parameters and pathname format letters, avoid using the letters (a, d, f, n, p, s, t, x, z) as FOR parameters or just choose a FOR parameter letter that is UPPER case.

    Format letters are case sensitive, so using a capital letter is also a good way to avoid conflicts, eg. %%A rather than %%a.

    Reference

    0 讨论(0)
  • 2021-01-15 21:52

    You could use the FOR /D command.

    for /d %%p in (foo*) do rd /s /q "%%p"
    
    0 讨论(0)
提交回复
热议问题