Sign in with
Sign up | Sign in
Your question
Solved

Delete folders in command promote with wildcards

Last response: in Windows 7
Share
April 5, 2011 9:09:32 PM

Is there a way to Delete multiple folders, and use wildcards as well in command prompt?
For example, I have a folder with hundreds of folder, and I want to delete all of them that start with a 9.

Thanks
a c 209 $ Windows 7
April 5, 2011 9:15:20 PM

rmdir 9* /s

add "/q" to suppress the confirmation prompt.


You can easily do this in Explorer as well - just sort by folder name, click on the first folder whose name starts with "9", shift-click on the last folder whose name starts with "9", and press the "Delete" key.
April 5, 2011 11:24:59 PM

sminlal said:
rmdir 9* /s

add "/q" to suppress the confirmation prompt.


You can easily do this in Explorer as well - just sort by folder name, click on the first folder whose name starts with "9", shift-click on the last folder whose name starts with "9", and press the "Delete" key.



Didn't work, I made a dummy folder with a bunch of folders, some starting with 9 and others with other numbers and letters.

It did rmdir 9* /s
and it get "The filename, directory name, or volume label syntax is incorrect".

I was thinking about putting this in a script, so I can run it easily one 30+ computers, that is why I don't want to do in in Explorer.
Related resources

Best solution

a c 209 $ Windows 7
April 6, 2011 1:21:50 AM
Share

Catsrules said:
It did rmdir 9* /s
and it get "The filename, directory name, or volume label syntax is incorrect".
Whoops, forgot that "rmdir" doesn't accept wildcards.

Try this:

for /D %f in (9*) do rmdir %f /s

That command should work if you type in in directly at the Command Prompt window. If you want to put it into a ".bat" or a ".cmd" file, you need to double the "%" characters, as in:

for /D %%f in (9*) do rmdir %%f /s

As above, add "/q" to suppress the confirmation prompts.
April 6, 2011 1:59:39 AM

It worked!!

So how does this fancy command work?
I am guessing %f is a variable or something?

a c 209 $ Windows 7
April 6, 2011 2:10:59 AM

Yeah basically the "for" command lets you execute whatever command you write after "do" multiple times, once for each file found in the "(...)" expression. The found file name is put into the "%f" variable and can be used as part of the command. "/D" tells it to look for directories - normally it will only find files.
April 6, 2011 5:05:44 AM

sminlal said:
Yeah basically the "for" command lets you execute whatever command you write after "do" multiple times, once for each file found in the "(...)" expression. The found file name is put into the "%f" variable and can be used as part of the command. "/D" tells it to look for directories - normally it will only find files.



Very cool!

Thanks for the Education.
April 6, 2011 5:06:01 AM

Best answer selected by Catsrules.
!