windows - Delete several directories with a common prefix at the command prompt

06
2014-04
  • Rajesh K Singh

    I want to delete several subdirectories of the same directory. The directories have "tmp" as the first three characters in their name. This is not working:

    C:\Documents and Settings\rajesh\Local Settings\Temp> rmdir tmp*
    
  • Answers
  • tenorkev

    The rmdir command does not accept wildcards. You can use a little for loop to achieve the same goal. This will run the rmdir command multiple times - once for each file/folder that matches the pattern given:

    for /D %f in (tmp*) do rmdir %f

    • Note if the folders are not empty, and you want it to recurse into them, you'd need to add /S on the end.
    • If you want to prevent the confirmation check, add /Q as well.
    • If you want to prevent it printing out the command for each line, add @ before the command.

    For example, with all of these options, you would have:

    for /D %f in (tmp*) do @rmdir %f /Q /S

    To see the full usage, type rmdir /?.

  • barlop

    a good safe general way is to make a file of the directories you want to delete dir /ad > blah Then check the file has what you want, then make a for command that echos every line of the file. for /f %a in (blah) do @echo %a then amend it for the commands you want e.g. this line puts rmdir at the beginning of each line of the file for /f %a in (blah) do @echo rmdir %a or at the end of that for add /q /s to the rmdir %a already there i.e. use @echo rmdir %a /q /s. And then once fine with what commands it would be running, remove the echo part so for /f %a in (blah) do @rmdir %a /q /s

    another way is also using that starting method of making the file listing the subdirectories to delete.. So each line of the file can be amended to read the command to delete that subdirectory. Then use sed on the file to replace subdirectory x with rmdir x /q /s.

    So if you have the file called blah. sed "s/.*/rmdir \0 \/q\/s/" blah >f type f see it has all the commands. to delete each subdirectory. if you're happy with it you want to execute it, you can rename f to f.bat and execute it.


  • Related Question

    command line - Recursively delete empty directories in Windows
  • mohlsen

    I have a directory on my Windows 7 machine that has hundreds if not thousands of sub-directories. Some of them have files, some do not. I want to delete all the empty directories.

    Looking at the del and rmdir DOS command, it does not look like you can recursively do this without deleting all the files. Is there a way to do this from the command line? Or is there a tool that would do it for me?


  • Related Answers
  • 8088

    You can use this utility: Remove Empty Directories

    Alternatively you can use this one-liner batch file:

    for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"
    

    One-liner taken from DownloadSquad, an excellent site to add to your RSS feeds. :)

  • 8088

    If you get the get the error:

    "%%d was unexpected at this time.
    

    it is likely you are running directly from the command line. In that case, change the double %% to a single %:

    for /f "delims=" %d in ('dir /s /b /ad ^| sort /r') do rd "%d"
    
  • Rob Kam

    The free utility EmptyFolderNuker does this fine, from a base folder of your choice. It also removes those directories only containing empty sub-directories.

  • 8088

    Since Cygwin comes with GNU find, you can do this:

    find . -type d -empty -delete
    

    Or to avoid the noise when a folder no longer exists:

    find . -type d -empty -execdir rmdir {} +
    
  • outsideblasts

    The excellent Glary Utilities has this and a bunch of other great features.

  • Nighthawk

    If you have Cygwin installed, you could do this:

    find -type d -exec rmdir {} \;