1

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*

2 Answers 2

5

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.
  • The quotes around %f will allow folders with spaces to be deleted.

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 /?.

4
  • and he should echo first. @echo rmdir ...
    – barlop
    Dec 20, 2013 at 11:40
  • 1
    In case folder have space characters you should use for /D %f in (tmp*) do @rmdir "%f" /Q /S
    – Adi Roiban
    Mar 11, 2015 at 13:51
  • -1 as this won't work for spaces in the file names. Need to add quotes to %f.
    – HippoDuck
    Jun 30, 2017 at 10:27
  • If you are running this command in a DOS Batch file then make sure to use double percents i.e. for /D %%f in (tmp*) do @rmdir %%f /Q /S
    – Sam B
    Dec 18, 2018 at 13:57
0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .